Home > Software design >  Serilog Expression Template in appSettings.json is being ignored
Serilog Expression Template in appSettings.json is being ignored

Time:06-27

I am new to Serilog and am trying to use the Expression Template syntax within my appSettings.Development.json file.

In my Program.cs class I initialise the bootstrap logger from code. This works as expected, logging to the console with a 'normal' output template. Inside the Startup.cs class I re-setup the logger with ReadFrom.Configuration(...).CreateLogger().

If my configuration looks as follows, it works correctly and logs to the console using the specified outputTemplate:

"WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
    }
  },...]

The Serilog "using" tag, common to the configuration, is:

"Using": [
  "Serilog.Sinks.Console",
  "Serilog.Sinks.Seq",
  "Serilog.Expressions"
],

If I change the WriteTo tag to be as follows the log format is not honoured and the default template is used (the template configured with the bootstrap logger in fact):

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  },...]

In trying to work out the solution I have looked at the question posed by mberube.net here but copying and pasting the code provided in that solution still doesn't log using the template format. My research to date indicates that the versions of the relevant Serilog packages I am using should be correct to enable this functionality.

My application is an ASP .Net Core Web API targetting net5.0. I have referenced Serilog.Expressions Version="3.4.0" and Serilog.Settings.Configuration Version="3.3.0".

I am hoping someone can assist me to understand why the templated output format is not being applied and what I can do to correct it?

If there is any further information that is required please let me know.

Thanks

CodePudding user response:

The blocks in appsettings.Development.json files don't override the ones in appsettings.json - they're merged in, so what you're most likely ending up with is:

 "WriteTo": [
  {
    "Name": "Console",
    "Args": {
      "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
      "formatter": {
        "type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
        "template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
      }
    }
  }

This won't play well with Serilog's configuration overload selection - it'll pick what it thinks is the simplest option among possible configuration methods.

A better idea is to have no sink configuration ("WriteTo" blocks) in the root appsettings.json file at all, and put separate configurations in appsettings.Development.json and appsettings.Production.json. It's very hard not to end up with unexpected results, otherwise.

  • Related