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.

Here’s a new Visual Studio Code-inspired theme:

// Install-Package Serilog.Sinks.Console -Pre

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.Console(theme: AnsiConsoleTheme.Code)
    .CreateLogger();

Log.Debug("Starting up");

var user = new { Name = Environment.UserName, Id = 42 };
Log.Information("Hello, {@User}!", user);

In Windows 10’s cmd.exe:

VS Code Console Theme

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
  • AnsiConsoleTheme.Grayscale - an ANSI version of the “grayscale” theme
  • AnsiConsoleTheme.Code - the VS Code-inspired theme above

Give it a try! Adding a new theme should be easy; check out the examples in the SystemConsoleThemes and AnsiConsoleThemes classes. If you come up with a theme you like (or spot some bugs) we’d love to hear from you over at the GitHub repository.