Jul 2019

.net code

Serilog is a structured logging library for .NET with more features and better performance than the built-in Microsoft logging libraries, but the standard console sink is still slow.

Console output is a bytestream managed by the underlying OS. Most IO classes (like FileStream) have async methods but the Console class has never been updated and so it always blocks the thread when writing. The Console.WriteLine method is also backed by a SyncTextWriter which uses a global lock to prevent multiple threads from writing partial lines. These two details cause a major bottleneck as multiple threads will be blocked waiting for each other to write to the same stream.

I released Serilog.Sinks.FastConsole as a new Serilog sink that uses System.Threading.Channels to buffer log statements and write them using an async writer directly to the console output stream. This massively improves logging performance for high-throughput programs. See more info in the readme.

dotnet add package Serilog.Sinks.FastConsole
var config = new FastConsoleSinkOptions { UseJson = true };
Log.Logger = new LoggerConfiguration().WriteTo.FastConsole(config).CreateLogger();