Search code examples
c#async-awaitserilogserilog-sinks-file

.Net Core 3.1 Asynchronous File Logging


I have a use case where I need to log data to file Asynchronously which I do not see it possible with Microsoft.Extensions.Logging so need your help to figure out the best solution

  1. Use Seri ILogger interface, which according to this github doc does not seem to be using async await implementation but rather uses a background worker thread.

The wrapped sink (File in this case) will be invoked on a worker thread while your application's thread gets on with more important stuff.

  1. Write My Custom Logger which utilizes TextWriter.Synchronized and WriteLineAsync

OR if there is any other solution which I am not aware of


Solution

  • It's normal for logging to use a background thread so it can expose a synchronous logging API. If all logging APIs were asynchronous, then that would force practically every method to be asynchronous.

    Logging synchronously to a background thread also enables buffering and batched writes, which increase performance.