3

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?

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
  • Does this answer your question? [Serilog MSSQL Sink doesn't write logs to database](https://stackoverflow.com/questions/52953060/serilog-mssql-sink-doesnt-write-logs-to-database) – C. Augusto Proiete May 06 '20 at 21:31

2 Answers2

0

There are a number of things you can do to troubleshoot issues in Serilog. Several are listed in this answer here on StackOverflow:

Serilog MSSQL Sink doesn't write logs to database

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
0

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;
Eric Snyder
  • 1,816
  • 3
  • 22
  • 46