Stuart Northcott
Stuart Northcott

Reputation: 141

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:

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.

Upvotes: 4

Views: 11105

Answers (1)

Stuart Northcott
Stuart Northcott

Reputation: 141

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...

Upvotes: 10

Related Questions