Log exception details and custom properties that are not output in Exception.ToString().
C# PowerShell Shell
Latest commit 198a5c9 Jan 7, 2017 @RehanSaeed committed on GitHub Merge pull request #15 from rsuter/master
Detect cyclic loops and add $ref and $id properties
Permalink
Failed to load latest commit information.
Images Community Icon Dec 7, 2015
Source Merge branch 'master' into master Jan 7, 2017
Tests/Serilog.Exceptions.Test
.gitignore Ignore Cake tools folder Sep 9, 2016
.travis.yml Update .travis.yml Dec 8, 2016
CONTRIBUTING.md Added Contributing guide Sep 16, 2016
Key.snk Restructured project Sep 16, 2016
LICENSE Initial commit Dec 7, 2015
MinimumRecommendedRulesWithStyleCop.ruleset Restructured project Sep 16, 2016
README.md
Serilog.Exceptions.sln Restructured project Sep 16, 2016
appveyor.yml Update appveyor.yml Dec 8, 2016
build.cake Use AppVeyor.yml to puck up artifacts Sep 21, 2016
build.ps1 rename back Sep 16, 2016
build.sh Add permissions to execute build.sh for Travis Sep 16, 2016
global.json

README.md

Serilog.Exceptions Logo Serilog.Exceptions

Serilog.Exceptions is an add-on to Serilog to log exception details and custom properties that are not output in Exception.ToString().

AppVeyor Build status Travis CI Build Status NuGet Package MyGet Package

What Does It Do?

Your JSON logs will now be supplemented with detailed exception information and even custom exception properties. Here is an example of what happens when you log a DbEntityValidationException from EntityFramework (This exception is notorious for having deeply nested custom properties which are not included in the .ToString()).

try
{
    ...
}
catch (DbEntityValidationException exception)
{
    logger.Error(exception, "Hello World");
}

The code above logs the following:

{
  "Timestamp": "2015-12-07T12:26:24.0557671+00:00",
  "Level": "Error",
  "MessageTemplate": "Hello World",
  "RenderedMessage": "Hello World",
  "Exception": "System.Data.Entity.Validation.DbEntityValidationException: Message",
  "Properties": {
    "ExceptionDetail": {
      "EntityValidationErrors": [
        {
          "Entry": null,
          "ValidationErrors": [
            {
              "PropertyName": "PropertyName",
              "ErrorMessage": "PropertyName is Required.",
              "Type": "System.Data.Entity.Validation.DbValidationError"
            }
          ],
          "IsValid": false,
          "Type": "System.Data.Entity.Validation.DbEntityValidationResult"
        }
      ],
      "Message": "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.",
      "Data": {},
      "InnerException": null,
      "TargetSite": null,
      "StackTrace": null,
      "HelpLink": null,
      "Source": null,
      "HResult": -2146232032,
      "Type": "System.Data.Entity.Validation.DbEntityValidationException"
    },
    "Source": "418169ff-e65f-456e-8b0d-42a0973c3577"
  }
}

Getting Started

Add the Serilog.Exceptions NuGet package to your project using the NuGet Package Manager or run the following command in the Package Console Window:

Install-Package Serilog.Exceptions

When setting up your logger, add the With.ExceptionDetails() line like so:

using Serilog;
using Serilog.Exceptions;

ILogger logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .WriteTo.Sink(new RollingFileSink(
        @"C:\logs",
        new JsonFormatter(renderMessage: true))
    .CreateLogger();

Performance

This library has custom code to deal with extra properties on most common exception types and only falls back to using reflection to get the extra information if the exception is not supported by Serilog.Exceptions internally.

Custom Exception Destructurers

You may want to add support for destructuring your own exceptions without relying on reflection. To do this, create your own destructuring class implementing ExceptionDestructurer (You can take a look at this for ArgumentException), then simply add it like so:

using Serilog;
using Serilog.Exceptions;

var exceptionDestructurers = new List<IExceptionDestructurer>();
exceptionDestructurers.AddRange(ExceptionEnricher.DefaultDestructurers);  // Add built in destructurers.
exceptionDestructurers.Add(new MyCustomExceptionDestructurer());          // Add your custom destructurer.

ILogger logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails(exceptionDestructurers)
    .WriteTo.Sink(new RollingFileSink(
        @"C:\logs",
        new JsonFormatter(renderMessage: true))
    .CreateLogger();

If you write a destructurer that is not included in this project (even for a third party library), please contribute it.

Contributing

Please look at the contributing guide.

Special Thanks

  • Jérémie Bertrand - For making Serilog.Exceptions compatible with Mono.
  • krajek - For writing some much needed unit tests.