serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
278 stars 148 forks source link

Q: Async serilog sink and performance #380

Closed sommmen closed 2 years ago

sommmen commented 2 years ago

Hiya,

Quick question - i'm seeing some locking happen in my app and it seems to be doing with logging. I use the following sinks:

I want to move the sinks into Serilog.Async and to keep it consistent i'm considering moving all sinks into the async package. I ofcourse could also only move File and Console to the async serilog sink and leave the MSSQL sink alone

Just a quick peer-check: Would there be any significant performance degradations in doing this? Would it be better to not put the MSSQL sink in the Async sink or would it not matter much?

Reading the docs:

If you initialize the sink with WriteTo then it uses a batched sink semantics. This means that it does not directly issue an SQL command to the database for each log call, but it collectes log events in a buffer and later asynchronously writes a bulk of them to the database using SqlBulkCopy.

I'd say there would be some overhead (given the double queueing but nothing major.

ckadluba commented 2 years ago

Hi @sommmen!

Sorry for the delay in response. Like the documentation states, if you use the MSSQL sink in normal mode (WriteTo()) then the log operation just writes into a buffer which is asynchronously written to the DB in batches in the background. I guess there would be no performance or resposibility gain in your app if you call that asynchronously. On the other hand doing so would not be a big problem either. Check out the code starting here for details: https://github.com/serilog-mssql/serilog-sinks-mssqlserver/blob/5e9dc3a5a470a35292dc80ab612c1eaac2c84fe6/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs#L125

If you use the MSSQL sink in audit mode (AuditTo()) then each log call is written to the database directly without any buffering or delay in the sink. If you call this async your app would continue executing while the log message is written to the DB but this would also defeat the purpose of the audit sink.

I hope this helps.

sommmen commented 2 years ago

Hi @sommmen!

Sorry for the delay in response. Like the documentation states, if you use the MSSQL sink in normal mode (WriteTo()) then the log operation just writes into a buffer which is asynchronously written to the DB in batches in the background. I guess there would be no performance or resposibility gain in your app if you call that asynchronously. On the other hand doing so would not be a big problem either. Check out the code starting here for details:

https://github.com/serilog-mssql/serilog-sinks-mssqlserver/blob/5e9dc3a5a470a35292dc80ab612c1eaac2c84fe6/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSink.cs#L125

If you use the MSSQL sink in audit mode (AuditTo()) then each log call is written to the database directly without any buffering or delay in the sink. If you call this async your app would continue executing while the log message is written to the DB but this would also defeat the purpose of the audit sink.

I hope this helps.

Hello @ckadluba,

Thanks for your answer - i'll be able to make an informed decision now.