Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change colors in serilog?

I have just integrated Serilog in my dot net core project. It is working really well but I use a dark theme and some logs are really dificult to read. As an example:

enter image description here

This is how I init Serilog:

string environment =  Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        
LoggerConfiguration loggerConfig = new LoggerConfiguration();
            
if (environment == "Production")
    loggerConfig.MinimumLevel.Information();
                
loggerConfig.MinimumLevel.Override("Microsoft.AspNetCore",
    LogEventLevel.Warning) 
    .Enrich.FromLogContext()
    .WriteTo.Console()
    .WriteTo.File("Logs/app.log");

Is there any way I could change colors to make that black logs white for example?

like image 682
Notbad Avatar asked Jul 17 '20 12:07

Notbad


People also ask

What is the use of Serilog?

Serilog is a . NET library that provides diagnostic logging to files, the console, and almost everywhere you would like. Serilog can be used in classic . NET Framework applications and for applications running on the latest and greatest .

What is Serilog sink?

Serilog provides sinks for writing log events to storage in various formats. Many of the sinks listed below are developed and supported by the wider Serilog community; please direct questions and issues to the relevant repository. More sinks can be found by searching within the serilog tag on NuGet.


1 Answers

Yes, the way to change colors when using the Console sink is through themes. You can try one of the built-in ones, or create your own.

The Console sink will colorize output by default:

Colorized Console

Themes can be specified when configuring the sink:

    .WriteTo.Console(theme: AnsiConsoleTheme.Code)

The following built-in themes are available as of this writing:

  • ConsoleTheme.None - no styling
  • 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 theme using only shades of gray, white, and black
  • AnsiConsoleTheme.Literate - an ANSI 16-color version of the "literate" theme; we expect to update this to use 256-colors for a more refined look in future
  • AnsiConsoleTheme.Grayscale - an ANSI 256-color version of the "grayscale" theme
  • AnsiConsoleTheme.Code - an ANSI 256-color Visual Studio Code-inspired theme

Adding a new theme is straightforward; examples can be found in the SystemConsoleThemes and AnsiConsoleThemes classes.

like image 176
C. Augusto Proiete Avatar answered Sep 23 '22 18:09

C. Augusto Proiete