Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured exception formatter (fixes #388) #433

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Structured exception formatter (fixes #388) #433

wants to merge 1 commit into from

Conversation

kmcclellan
Copy link

@kmcclellan kmcclellan commented Nov 19, 2021

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.

@RehanSaeed
Copy link
Owner

Polite ping to @krajek.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants