【问题标题】:Serilog ms sql sink not writing to my tableSerilog ms sql sink 没有写入我的表
【发布时间】:2020-05-07 06:27:18
【问题描述】:

Log.Information("Hello"); 没有写入表。我在自述文件中使用了基本配置here,并且接收器在第一次运行时创建了我的表。我确定我的用户具有读/写权限。

我期待Log.Information("Hello"); 向表中添加一行。

Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
var logDB = @"data source=xxxxxx\SQLEXPRESS;initial catalog=Eng;integrated security=False;persist security info=True;user id=xxxx;password=xxxxxxxx";
 var sinkOpts = new SinkOptions();
 sinkOpts.TableName = "SL24AddInLogging";
 sinkOpts.AutoCreateSqlTable = true;
 var columnOpts = new ColumnOptions();
 columnOpts.Store.Remove(StandardColumn.Properties);
 columnOpts.Store.Add(StandardColumn.LogEvent);
 columnOpts.LogEvent.DataLength = 2048;
 columnOpts.PrimaryKey = columnOpts.TimeStamp;
 columnOpts.TimeStamp.NonClusteredIndex = true;

 var log = new LoggerConfiguration()
     .WriteTo.MSSqlServer(
      connectionString: logDB,
      sinkOptions: sinkOpts,
      columnOptions: columnOpts
 ).CreateLogger();

我一定是做错了什么。什么?

【问题讨论】:

标签: serilog


【解决方案1】:

您可以对troubleshoot issues in Serilog 做很多事情。 StackOverflow 上的这个答案中列出了几个:

Serilog MSSQL Sink doesn't write logs to database

【讨论】:

    【解决方案2】:

    我发现了我的问题。过去,当我使用 Serilog 时,我使用的是静态“Log.Information()”,而不是创建记录器时在代码中声明的变量。在这种情况下,我声明的变量也是“log” - 小写。

    Log.Information() - 不好。

    log.Information() - 很好。

    编辑 另外,万一你遇到这个。我使用静态版本在整个项目中都可以使用我的日志记录。要定义记录器并使其可用,您可以使用以下模式:

    var log = new LoggerConfiguration()
     .WriteTo.MSSqlServer(
      connectionString: logDB,
      sinkOptions: sinkOpts,
      columnOptions: columnOpts
          ).CreateLogger();
    
    Log.Logger = log;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多