ANSI/Windows 10 terminal themes for Serilog

Jun 19, 2017 • nblumhardt

Nothing makes a long flight … fly by … like a fun programming project! On my way back from NDC Oslo on the weekend, I dug into custom console theming for Serilog, adding ANSI color support for a slicker experience on Linux, macOS, and Windows 10.

You can see how colorized output makes it much easier to read structured data/JSON in log output. This is an ANSI 256-color theme, supported by modern terminals on Linux and macOS. On Windows, you’ll need a recently-updated Windows 10 machine for this.

(.NET programs won’t show ANSI colors on Windows by default - the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag needs to be set with PInvoke first - thanks @khellang for the tip! You can see how it’s done portably for .NET Framework and .NET Core targets in the WindowsConsole helper class.)

So far, there are a handful of built-in themes:

ConsoleTheme.None - no styling; looks like the original Serilog.Sinks.Console output

SystemConsoleTheme.Literate - styled to replicate Serilog.Sinks.Literate, using the System.Console coloring modes supported on all Windows/.NET targets (this is the default when no theme is specified)

SystemConsoleTheme.Grayscale - a new theme, using only shades of gray, white, and black; I like the subtlety of this one

AnsiConsoleTheme.Literate - an ANSI 16-color version of the “literate” theme; added with the intention that we’ll update this to 256-colors for a more refined look