Search code examples
asp.net-coreserilogasp.net-core-3.0

Serilog WriteTo SQL Server - LoggerConfiguration issue


WriteTo option is not working, unable to see the log the message in database. Trying to find out what causing this issue. It shows recommended to go with WriteTo.

Also, what is the difference AuditTo vs WriteTo

  new LoggerConfiguration().ReadFrom
                           .Configuration(configObject)
                           .WriteTo
                           .Logger(log => log.WriteTo.MSSqlServer(connectionString, "LogMessages", columnOptions: logMessagesColumnOptions)
                           .Filter
                           .ByIncludingOnly(t => LogMessagesOptions.Contains(t.Level)))   
                           .Enrich.With(enrichersArray)
                           .Enrich.FromLogContext()
                           .CreateLogger();

Solution

  • There are a number of things you can do to help you troubleshoot why messages are not being written to the Serilog Sink. You can see several of them on another answer here in SO:

    Serilog MSSQL Sink doesn't write logs to database


    Re: WriteTo vs AuditTo the difference is reliability. If you use WriteTo and an error occurs, your application will continue to run (no exceptions will be thrown from Serilog) and log messages might be lost. If you use AuditTo and an error occurs, an exception will be thrown and your application will have to handle it. You can read more about Audit Logging here.