.netlogging.net-corethemesserilog

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?


Solution

  • 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:

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