0

I'm working with .Net Core 3.1 and Serilog but I have a small but annoying problem with the log output.

Configuration

My appsettings.json is

{
  "Serilog": {
    "Properties": {
      "Application": "Testing GZip"
    },
    "WriteTo": [
      {
        "Name": "Debug",
        "Args": {
          "outputTemplate": "[{Timestamp:o}] [{Level,3:u}] [{MachineName}/{ProcessName}:{ProcessId}/{ThreadName}:{ThreadId}] [{Application}/{SourceContext}] {Message}{NewLine}{Exception}{Properties:j}"
        }
      }
    ],
    "Enrich": [ "WithMachineName", "WithProcessName", "WithProcessId", "WithThreadName", "WithThreadId", "WithExceptionDetails" ]
  }
}

The problem

As you can see, my outputTemplate ends with {NewLine}{Exception}{Properties:j} and this is mandatory if you want to log exceptions and exception details.

Unfortunately, I noticed that the output is not well-formatted. Have a look here:

[2020-04-10T13:58:16.5592267+02:00] [INFORMATION] [PRO4-AEG/Test_Serilog_Logging:8096/:1] [Testing GZip/object] Start logging
{}[2020-04-10T13:58:16.6547724+02:00] [INFORMATION] [PRO4-AEG/Test_Serilog_Logging:8096/:1] [Testing GZip/object] Test1
{}Exception thrown: 'System.ArgumentNullException' in Test_Serilog_Logging.dll
{}[2020-04-10T13:58:16.6755158+02:00] [ERROR] [PRO4-AEG/Test_Serilog_Logging:8096/:1] [Testing GZip/object] You wrote 'THIS IS A TEST!' - Test_Serilog_Logging.TestClass
System.ArgumentNullException: Value cannot be null. (Parameter 'argument')
   at Test_Serilog_Logging.TestClass.TestLogging() in C:\Users\attil\source\repos\Test_Serilog_Logging\Test_Serilog_Logging\TestClass.cs:line 25
{"ExceptionDetail":{"Type":"System.ArgumentNullException","HResult":-2147467261,"Message":"Value cannot be null. (Parameter 'argument')","Source":"Test_Serilog_Logging","ParamName":"argument"}}[2020-04-10T13:58:16.7337262+02:00] [INFORMATION] [PRO4-AEG/Test_Serilog_Logging:8096/:1] [Testing GZip/object] Stop logging
{}The program '[8096] Test_Serilog_Logging.exe' has exited with code 0 (0x0).

As you can see after the first log event, Serilog starts to prefix the row with the two characters {}, that disappears if I delete the {Properties:j} from my configuration file.

Any ideas to solve the problem?

Thank you. Attilio

Attilio Gelosa
  • 555
  • 1
  • 8
  • 23

2 Answers2

2

The {} at the start of each row is the content of your Properties as JSON (an empty object) because you have no properties defined for these log messages. It appears at the beginning of the next line because of how you defined the output template:

{Message}{NewLine}{Exception}{Properties:j}

This is causing a {NewLine} to be added before the exception details and the properties.

If you add the {NewLine} to the end of your output template instead, you'll get the {} on the same line...

{Message}{Exception}{Properties:j}{NewLine}
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
2

One option you might consider is to use a Serilog formatter such as the Serilog.Formatting.Json.JsonFormatter or Serilog.Formatting.Compact.CompactJsonFormatter - see Serilog wiki for more information about formatting output, as well as image below about how to configure this your appsettings.json file. Alternatively you could create your own custom formatter - see CompactJsonFormatter for an example.

enter image description here

Ryan.Bartsch
  • 3,698
  • 1
  • 26
  • 52