I have created a .Net class library (4.6.2) and created serilog implementation which is called by other interfaces such as console app. Now when I use File sink type, the lo
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
.
In my case it was because my database user didn't have the necessary permissions to log to the database schema.table.
I ended up doing something like the following:
/* CREATE A NEW ROLE */
CREATE ROLE db_logger
/* GRANT SELECT, INSERT TO THE ROLE */
GRANT SELECT, INSERT ON [Logging].[Logs] TO db_logger --Allows SeriLog to insert
Curiously "WriteTo" needs the SELECT permissions but "AuditTo" does not. Both methods need the INSERT permission.