serilog-sinks-mssqlserver: Serilog.Sinks.MsSqlServer stops writing to database

I’m using:

  • Asp.Net Core 2.2.0
  • Serilog 2.9.0
  • Serilog.AspNetCore 3.2.0
  • Serilog.Sinks.MsSqlServer 5.1.3

I’m configuring Serilog in appsettings.json :

  "Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "MSSqlServer",
        "Args": {
          "connectionString": "Server=MyServer;Database=MyDatabase;Integrated Security=SSPI;",
          "schemaName": "MySchema",
          "tableName": "Log",
          "autoCreateSqlTable": "true"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": ".\\Logs\\MyApp.log",
        }
      }
    ]
  },

In my Startup.cs :

Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();

Log.Information("Starting application ...");

I see the initial log written to both file and database.

But subsequent Log.Information() statements are written only to file – not to database.

There is nothing in my code to change Serilog config. I don’t see why initial log would be written to database but not subsequent.

I’ve configured self-log

SelfLog.Enable(msg => Debug.WriteLine(msg));

but Serilog is not throwing any exceptions ( or at least it’s not writing them to debug output ).

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 37 (17 by maintainers)

Most upvoted comments

I’m assuming the BatchPeriod expires while waiting for input, forcing the records to be flushed. Is there any way to manually flush the buffer?

Yes, there is a way. You have to call Log.CloseAndFlush(); as documented here.

https://github.com/serilog/serilog-sinks-mssqlserver#always-call-logcloseandflush