An extension for the great Serilog Sinks Console which provides the following extra features:
Make sure to star this project as a thank you, it makes me feel good about myself :P
There are two types of themes:
SystemConsoleThemes
- These are basically legacy themes with better overall terminal support, these however only support a Foreground and Background color in 16 colorsAnsiThemes
- These are modern themes that support everything between 16 colors, 256 colors and Truecolor (24-bit RGB) with additional log formatting, think Bold, Italic, Underline etcHow a specific theme looks in your terminal of your IDE of choice, can vary wildly so to get a quick demo how each theme looks in your IDE, take a look at LogTheme demo
To use Serilog.Sinks.Console.LogThemes
:
dotnet add package Serilog.Sinks.Console.LogThemes
Note: Serilog.Sinks.Console.LogThemes
already has a dependency on Serilog.Sinks.Console.LogThemes
, so it's not necessary to install both.
Then enable the sink using WriteTo.Console()
and select a theme from LogThemes
:
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(theme: LogThemes.Sixteen) // <= Select theme here
.CreateLogger();
Log.Information("Hello, world!");
Note: I initially planned on making screenshots for everything but how you will see every theme in your specific IDE and terminal, with your specific settings, will be completely different from the screenshots anyway.
The following built-in themes are available through the LogThemes
class:
Ansi Themes
LogThemes.Empty
- no styling, just plain white/black loggingLogThemes.Code
- an ANSI 256-color Visual Studio Code-inspired themeLogThemes.Grayscale
- an ANSI 256-color version of the "grayscale" themeLogThemes.Literate
- an ANSI 256-color version of the "literate" themeLogThemes.Sixteen
- an ANSI 16-color theme that works well with both light and dark backgroundsLogThemes.SixteenNonBright
- Extended from LogThemes.Sixteen
with the log level background colors muted.System Console Themes
LogThemes.SystemColored
- Basic grayscale logging except for the background of the log levelsLogThemes.SystemGrayscale
- a theme using only shades of gray, white, and blackLogThemes.SystemLiterate
- styled to replicate Serilog.Sinks.Literate, using the System.Console
coloring modes supported on all Windows/.NET targets;
Wondering how each theme looks in your IDE?
Well wonder no further because you have the following options to quickly check how everything looks:
Note: the following can be placed anywhere but ideally in you Program.cs Main function and should NEVER be pushed to production.
// Predefined theme test
await LogThemeDemo.TestTheme(LogThemes.Sixteen);
// Custom theme test
var theme = LogThemes.UseAnsiTheme<CustomCodeThemeTemplate>();
await LogThemeDemo.TestTheme(theme);
// This will print out every theme with the corresponding Theme name
// so you can pick and choose what you like
await LogThemeDemo.TestAllThemes();
await LogThemeDemo.TasteTheRainbow(config =>
{
// Optional config
config.Loops = 30;
config.RandomForeground = true;
config.RandomBackground = false;
config.RandomFormatType = true;
config.UseColor256 = false;
config.DelayBetweenLogs = 50;
});
All themes can be extended and overwritten on a per property basis, or essentially be created from scratch by extending either the BaseSystemConsoleTheme or BaseAnsiTheme
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(theme: LogThemes.UseAnsiTheme<CustomCodeThemeTemplate>()) // <= Select ANSI theme here
.WriteTo.Console(theme: LogThemes.UseSystemConsoleTheme<CustomCodeThemeTemplate>()) // <= Or select System Console theme here
.CreateLogger();
Inside every theme you have a powerful LogTheme
(ANSI) and LogSystemTheme
(System Console) class that can setup a style to however you would like it!
public class CustomCodeThemeTemplate : CodeAnsiThemeTemplate
{
// Use the LogTheme class to set the Foreground(), Background() and or FormatType()
protected override string Text { get; } = LogTheme.Foreground(Color256.Magenta164);
protected override string LevelDebug => LogTheme.Background(Color.CadetBlue).FormatType(FormatTypeEnum.BoldMode);
// You can also use a simple Style() constructor to set the properties in one go
protected override string Name => LogTheme.Style(Color.Salmon, Color.Azure);
// Or use short hand functions for setting the format
protected override string LevelError => LogTheme.Bold().Italic().Strikethrough().Foreground(Color.Red);
}
PR's are very welcome, got a cool idea for a theme than just create a PR for it!