Serilog ms sql sink not writing to my table

Viewed 484

The Log.Information("Hello"); is not writing to the table. I have used the basic configuration here in the readme file and the sink created my table OK on first run. I am certain that my user has read/write permission.

I am expecting the Log.Information("Hello"); to add a row to the table.

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

I must be doing something wrong. What?

2 Answers

I found my issue. In the past when I have used Serilog I have used the static "Log.Information()" rather than my variable that is declared in my code when I create the logger. In this case my declared variable is also "log" - lower case.

Log.Information() - bad.

log.Information() - good.

Edit In addition, in case you run into this. I have used the static version to have my logging available throughout the entire project. To define a logger and have it available you can use the following pattern:

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

Log.Logger = log;
Related