Serilog MSSQL Sink doesn’t write logs to database

Below are some ideas that could help you troubleshoot:


Are you testing with Verbose or Debug events only? That could be the reason. You didn’t specify a global minimum level for Serilog (you only specified for the minimum level for the sink, which acts as a filter), and the default minimum is Information, which means Verbose and Debug are being ignored… Specify the global MinimumLevel for Serilog:

ILogger logger = new LoggerConfiguration()
      .MinimumLevel.Verbose()
      .WriteTo.MSSqlServer(connectionString,
                           tableName,
                           autoCreateSqlTable: autoCreateSqlTable,
                           restrictedToMinimumLevel: LogEventLevel.Verbose,
                           columnOptions: GetSQLSinkColumnOptions(),
                           batchPostingLimit: batchPostingLimit)          
      .CreateLogger();

Are you disposing your logger? Serilog.Sinks.MSSqlServer is a “periodic batching sink”, so you’ll need to make sure you dispose the logger at the end to force it to flush the logs to the database. See Lifecycle of Loggers.

((IDisposable) logger).Dispose();

Even though you’re using 1 for batchPostingLimit, it waits 5 seconds by default before sending the logs to the database. If your app closes before that period and you didn’t dispose the logger, the messages are lost.


For the sake of troubleshooting, use AuditTo instead of WriteTo (and remove the batchPostingLimit which is not applicable for auditing). WriteTo is safe and will eat any exceptions, whilst AuditTo will let exceptions bubble up.

ILogger logger = new LoggerConfiguration()
    .AuditTo.MSSqlServer(
        connectionString,
        tableName,
        restrictedToMinimumLevel: LogEventLevel.Verbose,
        autoCreateSqlTable: true)
    .CreateLogger();

Of course, once you figure out what’s wrong, go back to WriteTo.


Leave a Comment