Serilog.Exceptions icon indicating copy to clipboard operation
Serilog.Exceptions copied to clipboard

Structured exception formatter (fixes #388)

Open kmcclellan opened this issue 4 years ago • 1 comments

From #388:

Exception.StackTrace is logged in Serilog and is not owned by our project. There are other Serilog packages that can allow you to omit it. If you'd like Serilog.Exceptions to do the same, we'd take a PR.

It's common for Serilog sinks to use ITextFormatter to control their behavior. Standard formatters (Serilog.Formatting.Json.JsonFormatter, Serilog.Formatting.Compact.CompactJsonFormatter) print out ExceptionDetails correctly, but they also do their own formatting of the exception. Thus, a custom JSON formatter that avoids exception.ToString() and uses the enriched properties instead seems useful for this library.

Example of usage:

Log.Logger = new LoggerConfiguration()
  .Enrich.WithExceptionDetails()
  .WriteTo.File(new StructuredExceptionFormatter(), "./logs.json")
  .CreateLogger();

appsettings.json:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "./logs.json",
          "formatter": "Serilog.Exceptions.Formatting.StructuredExceptionFormatter, Serilog.Exceptions"
        }
      }
    ]
  }
}

See also Stack Overflow.

kmcclellan avatar Nov 19 '21 02:11 kmcclellan

Polite ping to @krajek.

RehanSaeed avatar Feb 19 '22 12:02 RehanSaeed