3

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.

  • I would suggest to create a question issue in https://github.com/serilog/serilog-aspnetcore repo – Mike Feb 06 '18 at 15:29
  • 1
    Thanks for the suggestion Mike, I was worried that I was doing something wrong. Following your advice I've raised a new question/issue: https://github.com/serilog/serilog-aspnetcore/issues/31 I will update this post if/when I hear anything back. – Stuart Northcott Feb 07 '18 at 10:19

1 Answers1

9

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