serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
446 stars 129 forks source link

Configuring Console with theme overrides ExpressionTemplate #350

Open mpiliszcz opened 1 year ago

mpiliszcz commented 1 year ago
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "System": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": {
            "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
            "template": "[{@t:HH:mm:ss}{#if CorrelationId is not null} {Substring(CorrelationId, 0,5)}{#end} {@l:u3}] {#if SourceContext is not null}{SourceContext}{#end}{@m}\n{@x}"
          }//,
          //"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Literate, Serilog.Sinks.Console"
        }
      }
    ],
    "Enrich": [ "WithCorrelationId" ]
  },

If I remove commented out theme definition, the formatter will no longer be used, only theme will be finally configured. We can't have both.

0xced commented 1 year ago

Indeed you can't have both, but that's how the Serilog.Sinks.Console package works. This is not related to this Serilog.Settings.Configuration package.

If you write it programmatically you can configure either the theme:

configuration.WriteTo.Console(theme: AnsiConsoleTheme.Code);

or the formatter:

configuration.WriteTo.Console(formatter: new ExpressionTemplate("[{@t:HH:mm:ss}{#if CorrelationId is not null} {Substring(CorrelationId, 0,5)}{#end} {@l:u3}] {#if SourceContext is not null}{SourceContext}{#end}{@m}\n{@x}"));

But you can't have both, this does not even compile:

configuration.WriteTo.Console(
    theme: AnsiConsoleTheme.Literate,
    formatter: new ExpressionTemplate("[{@t:HH:mm:ss}{#if CorrelationId is not null} {Substring(CorrelationId, 0,5)}{#end} {@l:u3}] {#if SourceContext is not null}{SourceContext}{#end}{@m}\n{@x}")
);

Does this answer address your issue?