2

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();
user3711357
  • 1,425
  • 7
  • 32
  • 54
  • 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 Apr 15 '20 at 18:46

2 Answers2

2

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.

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

I had a similar experience with logging to the database using Serilog. I realised that logs were not being written until after the browser is closed i.e. when Log.CloseAndFlush() is called. This was a problem for me as the next task after calling the logging method was depending on the log being written immediately because it retrieves the logged record from the database. However, when it tries to get the supposedly logged record, it gets null because the logging is delayed until the program terminates. I was initially using WriteTo and decided to use AuditTo instead. This fixed the issue immediately. The main difference between AuditTo and WriteTo is that AuditTo ensures that a logging event succeeds before the next task is executed but WriteTo does not. Also, WriteTo fails silently i.e. if there's an issue preventing logging, the next task executes without an exception being thrown. AuditTo does not do that. If it is unable to write a log for any reason, an exception is thrown. AuditTo is best for situations where you cannot afford to lose a record and the order of logging is important.

oluwaremieo
  • 41
  • 1
  • 7