Serilog WriteTo MsSqlServer - LoggerConfiguration for synchronous insert

I recently had a problem using Serilog in a web application. The problem was caused by Serilogs default behavior when entering the database, so as not to insert the log immediately, but inserting it into the receiver and waiting for a certain amount of time or several logs before inserting them into the database. This, in my opinion, is asynchronous behavior.

What I wanted to do was set up my logger to insert logs into the database without waiting (synchronously).

The solution I found was as follows:

var logger = new LoggerConfiguration() .WriteTo .MSSqlServer(connectionString, tableName, columnOptions: columnOptions, autoCreateSqlTable: true, period: new TimeSpan(0, 0, 0)) .CreateLogger(); 

which, as you can see, means that I am waiting for the Timespan period (0, 0, 0), which is 0, before flushing the receiver into the database.

Has anyone found an alternative solution? Is there a reason why the solution above is not โ€œbest practiceโ€?

Thank you in advance!

+5
source share

Source: https://habr.com/ru/post/1263782/


All Articles