Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serilog JsonFormatter in an ASP.Net Core 2 not being applied from appsettings file

I have an ASP.Net Core 2 web application where I'm trying to configure SeriLog to output in JSON format, configured via an appsettings.json file. I cannot get this to work with either the standard Serilog.Formatting.Json.JsonFormatter or the compact Serilog.Formatting.Compact.CompactJsonFormatter.

I see there's a similar question asked here, but there seems to be no conclusive answer using appsettings.json, and the thread has gone quiet for over a year (and I don't have enough reputation to comment to nudge further responses).

I'm using the following SeriLog packages:

  • Serilog.AspNetCore - 2.1.0
  • Serilog.Enrichers.Environment - 2.1.2
  • Serilog.Enrichers.Thread - 3.0.0
  • Serilog.Formatting.Compact - 1.0.0
  • Serilog.Settings.Configuration - 2.4.0
  • Serilog.Sinks.RollingFile - 3.3.0

The SeriLog section of my appsettings.json file reads:

"Serilog": {
  "Using": ["Serilog.Sinks.RollingFile"],
  "MinimumLevel": {
    "Default": "Verbose",
    "Override": {
      "Microsoft": "Warning",
      "System": "Warning"
    }
  },
  "WriteTo": [
    {
      "Name": "RollingFile",
      "Args": {
        "pathFormat": "C:\\Logs\\MLSBPortal-{Date}.txt",
        "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
        "restrictedToMinimumLevel": "Debug",
        "retainedFileCountLimit": "10",
        "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{ThreadId}] [{SourceContext}] [{SessionId}] [{UserName}]: {Message}{NewLine}{Exception}"
      }
    }
  ],

with the formatter line modified to the following when testing with the default JSON formatter:

"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",

As suggested in multiple other places.

My output is being written fine, but the formatter setting does not appear to be making any difference to the output.

Am I doing something wrong here, or is this functionality not implemented via a configuration file read yet?

Thanks in advance for any help.

like image 627
Stuart Northcott Avatar asked Sep 10 '25 20:09

Stuart Northcott


1 Answers

It turns out that it is not (currently) possible to specify both a formatter and outputTemplate at the same time (as clarified at https://github.com/serilog/serilog-aspnetcore/issues/31 ).

Hopefully this situation may change in the future...

like image 93
Stuart Northcott Avatar answered Sep 13 '25 09:09

Stuart Northcott