JasonLandbridge / Serilog.Sinks.Console.LogThemes

An extension for the great Serilog.Sinks.Console which provides many extra features!
MIT License
4 stars 1 forks source link

Serilog.Sinks.Console.LogThemes

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

What you should know

There are two types of themes:

How 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

Getting started

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!");

Themes

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

System Console Themes

Demo

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.

Test a single theme:

// Predefined theme test
await LogThemeDemo.TestTheme(LogThemes.Sixteen);

// Custom theme test
var theme = LogThemes.UseAnsiTheme<CustomCodeThemeTemplate>();
await LogThemeDemo.TestTheme(theme);

Test all predefined themes:

// This will print out every theme with the corresponding Theme name 
// so you can pick and choose what you like
await LogThemeDemo.TestAllThemes();

Test various random colors and theme settings

await LogThemeDemo.TasteTheRainbow(config =>
{
    // Optional config
    config.Loops = 30;
    config.RandomForeground = true;
    config.RandomBackground = false;
    config.RandomFormatType = true;
    config.UseColor256 = false;
    config.DelayBetweenLogs = 50;
});

Custom Theme

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

  1. Extend either the BaseSystemConsoleTheme or BaseAnsiTheme, or any other predefined themes
  2. Override the properties that you want changed
  3. Set the theme in the logger config:
    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();

Fluent styling

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);
}

Resources

Contributions

PR's are very welcome, got a cool idea for a theme than just create a PR for it!