log4net 创建 sqlite db 但不写入它

log4net creates sqlite db but does not write to it

我尝试将 log4net 与定义一起使用来自这个 post:log4net config SqLite in code

我在那里使用静态class(减去测试方法):

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();
                }
            }
        }
    }
}

并且我在 Home 视图中的 ActionResult 方法中调用日志记录方法:

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();
        }

        ...

正在应用的 App_Data 目录中创建 sqlite 数据库,但正在插入 none 的日志写入。据我所知,使用调试器没有任何错误。

我看到其他 post 是关于给 IIS 'user' 将 perms 写入 db 目录的,但它显然需要创建 perms。

想法?

EDIT 我修改了 SqLiteAppender 方法以反映当前的工作条件。首先,根据下面的建议,我添加了 appender.BufferSize = 1;,然后更改了与 "message" 字段关联的布局。出于某种原因,Layout2RawLayoutAdapter 版本比我最初使用的更好。

ado 提供程序有一个默认缓冲区,可以同时写入多条消息。您可以更改缓冲区:

appender.BufferSize = 1;

然后所有消息将直接写入。