Structured exception formatter (fixes #388)
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.
Polite ping to @krajek.