Output with parameters in Serilog does no work

27 views Asked by At

In a .Net Core Webapi project I am using Serilog to log. But when I use _logger.LogInformation("Token for user {UserName} created",data.username); and check the log file, the output is "Token for user {Username} created". The {Username} is not replaced with the real value. I am sure that the value is not null.

I use this in a LoginService and the Serilog setting in the appsetting.json is

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
  "MinimumLevel": {
    "Default": "Debug",
    "Override": {
      "Microsoft": "Information",
      "System": "Information"
    }
  },
  "WriteTo": [
    { "Name": "Console" },
    {
      "Name": "File",
      "Args": {
        "path": "C:\\Users\\ab\\Projects\\IQC\\IQC_Log.txt",
        "rollingInterval": "Day",
        "rollOnFileSizeLimit": true,
        "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter"
      }
    }
  ]
}

Could someone help to point out where I did wrong?

1

There are 1 answers

0
Nicholas Blumhardt On

Assuming you're getting JSON in that file as expected, the problem will be that you're using CompactJsonFormatter, which does not render message templates.

Instead, you need to pass Serilog.Formatting.Compact.RenderedCompactJsonFormatter for the formatter arg.