Title: log4net creates sqlite db but does not write to it
Tags: c#;sqlite;log4net
Question: My attempt to use log4net with the definitions comes from this post: log4net config SqLite in code
I have the static class used there (minus the test method):
namespace My_App.Logging {
public class SqLiteLogging {
public static class SqLiteAppender {
public static IAppender GetSqliteAppender(string dbFilename) {
var dbFile = new FileInfo(dbFilename);
if (!dbFile.Exists) {
CreateLogDb(dbFile);
}
var appender = new AdoNetAppender {
ConnectionType = "System.Data.SQLite.SQLiteConnection, System.Data.SQLite",
ConnectionString = $"Data Source={dbFilename};Version=3;",
CommandText =
"INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)"
};
appender.AddParameter(new AdoNetAppenderParameter {
ParameterName = "@Date",
DbType = DbType.DateTime,
Layout = new RawTimeStampLayout()
});
appender.AddParameter(new AdoNetAppenderParameter {
ParameterName = "@Level",
DbType = DbType.String,
Layout = new Layout2RawLayoutAdapter(new PatternLayout("%level"))
});
appender.AddParameter(new AdoNetAppenderParameter {
ParameterName = "@Logger",
DbType = DbType.String,
Layout = new Layout2RawLayoutAdapter(new PatternLayout("%logger"))
});
appender.AddParameter(new AdoNetAppenderParameter {
ParameterName = "@Message",
DbType = DbType.String,
Layout = new Layout2RawLayoutAdapter(new PatternLayout("%message"))
});
appender.Threshold = log4net.Core.Level.All;
appender.BufferSize = 1;
appender.ActivateOptions();
return appender;
}
public static void CreateLogDb(FileInfo file) {
using (var conn = new SQLiteConnection()) {
conn.ConnectionString = $"Data Source={file.FullName};New=True;Compress=True;Synchronous=Off";
conn.Open();
using (var cmd = conn.CreateCommand()) {
cmd.CommandText = @"
CREATE TABLE IF NOT EXISTS Log(
LogId     INTEGER PRIMARY KEY,
Date      DATETIME NOT NULL,
Level     VARCHAR(50) NOT NULL,
Logger    VARCHAR(255) NOT NULL,
Message   TEXT DEFAULT NULL
cmd.ExecuteNonQuery();
}
conn.Close();
}
}
}
and I call the logging methods in an ActionResult method in my Home view:
namespace My_App.Controllers {
public class HomeController : Controller {
private static ILog _log;
public ActionResult Index() {
var dbFile = AppDomain.CurrentDomain.GetData("DataDirectory") + "/log4net.sqlite";
BasicConfigurator.Configure(SqLiteLogging.SqLiteAppender.GetSqliteAppender(dbFile));
_log = LogManager.GetLogger(typeof(HomeController));
_log.Info("Hello there");
return View();
}
...
The sqlite db is being created in the app's App_Data dir but none of the log writes are being inserted.  Nothing is throwing errors as far as I can tell using the debugger.
I've seen other posts about giving the IIS 'user' write perms to the db dir but it obviously has perms to create.
Ideas?
EDIT I modofied the SqLiteAppender method to reflect current working conditions. First, based on advice below I added appender.BufferSize = 1; then I changed the Layout associated with the "message" field.  For some reason the Layout2RawLayoutAdapter version works better than what I had originally.
Comment: enabled trace output for log4net and see it complaining about a RepositoryAttribute not being defined.  will try that

Comment: setting RepositoryAttribute did nothing useful, or I don't understand that well enough.  I set the threshold level to All and that doesn't seem to do anything either

Comment: Good catch though it doesn't seem to have had an effect.  I'm just using trace at the moment.

Comment: One thing to notice is the difference in your connection string when creating your db and when configuring for log4net: You create using `FileInfo.Fullname` but only use the `dbFilename` string when configuring. Might be worth a try to change `{dbFilename}` to `{dbFile.Fullname}` in your log4net connection string.

Comment: I only ever used log4net configured with web.config or app.config so I cannot tell what undefined *RepositoryAttribute* it could be complain about.

Here is the accepted answer: The ado provider has a default buffer to write multiple messages at the same time. You can change the buffer with:
appender.BufferSize = 1;
Then all messages will be written directly.
Comment for this answer: If you lots of logging, then you get better performance with a larger buffer


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:03/01/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><sqlite><log4net>', 'Title': 'log4net creates sqlite db but does not write to it', 'ViewCount': 384, 'id': 42540370, 'user_AboutMe': '<p>Blah, blah, blah</p>\n', 'user_AccountId': 2317284.0, 'user_DisplayName': '7 Reeds', 'user_Reputation': 2253.0, 'user_Views': 392.0, 'user_id': 2034048.0}}