serilog-exceptions - Serilog.Exceptions 4.0.0-beta-0160

Log exception details and custom properties that are not output in Exception.ToString().

PM> Install-Package Serilog.Exceptions -Version 4.0.0-beta-0160 -Source https://www.myget.org/F/serilog-exceptions/api/v3/index.json

Copy to clipboard

> nuget.exe install Serilog.Exceptions -Version 4.0.0-beta-0160 -Source https://www.myget.org/F/serilog-exceptions/api/v3/index.json

Copy to clipboard

> dotnet add package Serilog.Exceptions --version 4.0.0-beta-0160 --source https://www.myget.org/F/serilog-exceptions/api/v3/index.json

Copy to clipboard
<PackageReference Include="Serilog.Exceptions" Version="4.0.0-beta-0160" />
Copy to clipboard
source https://www.myget.org/F/serilog-exceptions/api/v3/index.json

nuget Serilog.Exceptions  ~> 4.0.0-beta-0160
Copy to clipboard

> choco install Serilog.Exceptions --version 4.0.0-beta-0160 --source https://www.myget.org/F/serilog-exceptions/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "serilog-exceptions" -SourceLocation "https://www.myget.org/F/serilog-exceptions/api/v2"
Install-Module -Name "Serilog.Exceptions" -RequiredVersion "4.0.0-beta-0160" -Repository "serilog-exceptions" -AllowPreRelease
Copy to clipboard

Schema.NET Banner

NuGet Package Serilog.Exceptions package in serilog-exceptions feed in Azure Artifacts Twitter URL Twitter Follow

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

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 WithExceptionDetails() line like so:

using Serilog;
using Serilog.Exceptions;

ILogger logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .WriteTo.RollingFile(
        new JsonFormatter(renderMessage: true), 
        @"C:\logs\log-{Date}.txt")    
    .CreateLogger();

Make sure that the sink's formatter outputs enriched properties. Serilog.Sinks.Console and many more do not do that by default. You may need to add {Properties:j} to your sink's format template. For example, configuration for console sink may look like that:

.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception} {Properties:j}")

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. Reflection overhead is present but minimal, because all the expensive relection-based operations are done only once per exception-type.

Additional Destructurers

Serilog.Exceptions.SqlServer

NuGet Package Serilog.Exceptions.SqlServer package in serilog-exceptions feed in Azure Artifacts

Add the Serilog.Exceptions.SqlServer NuGet package to your project to avoid the reflection based destructurer for SqlException when using System.Data.SqlClient:

Install-Package Serilog.Exceptions.SqlServer

Add the SqlExceptionDestructurer during setup:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
    .WithDefaultDestructurers()
    .WithDestructurers(new[] { new SqlExceptionDestructurer() }))

Serilog.Exceptions.MsSqlServer

NuGet Package Serilog.Exceptions.MsSqlServer package in serilog-exceptions feed in Azure Artifacts

Add the Serilog.Exceptions.MsSqlServer NuGet package to your project to avoid the reflection based destructurer for SqlException when using Microsoft.Data.SqlClient:

Install-Package Serilog.Exceptions.MsSqlServer

Add the SqlExceptionDestructurer during setup:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
    .WithDefaultDestructurers()
    .WithDestructurers(new[] { new SqlExceptionDestructurer() }))

Serilog.Exceptions.EntityFrameworkCore

NuGet Package Serilog.Exceptions.EntityFrameworkCore package in serilog-exceptions feed in Azure Artifacts

WARNING: If you are using EntityFrameworkCore with Serilog.Exceptions you must add this, otherwise in certain cases your entire database will be logged! This is because the exceptions in Entity Framework Core have properties that link to the entire database schema in them (See #100, aspnet/EntityFrameworkCore#15214)

Add the Serilog.Exceptions.EntityFrameworkCore NuGet package to your project when using EntityFrameworkCore in your project

Install-Package Serilog.Exceptions.EntityFrameworkCore

Add the DbUpdateExceptionDestructurer during setup:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
    .WithDefaultDestructurers()
    .WithDestructurers(new[] { new DbUpdateExceptionDestructurer() }))

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:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
    .WithDefaultDestructurers()
    .WithDestructurers(new[] { new MyCustomExceptionDestructurer() }))

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

Additional configuration

You can configure some additional properties of destructuring process, by passing custom destructuring options during setup:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
    .WithDefaultDestructurers()
    .WithRootName("Exception"))

Currently following options are supported:

  • RootName: The property name which will hold destructured exception, ExceptionDetail by default.
  • Filter: The object implementing IExceptionPropertyFilter that will have a chance to filter properties just before they are put in destructured exception object. Go to "Filtering properties" section for details.
  • DestructuringDepth: The maximum depth of reflection based recursive destructuring process.
  • ReflectionBasedDestructurer: Reflection based destructurer is enabled by default, but can be disabled in case you want to have complete control over destructuring process. You will have to register destructurers for all exceptions explicitly.

Filtering properties

You may want to skip some properties of all or part your exception classes without directly creating or modifying custom destructurers. Serilog.Exceptions supports this functionality using a filter.

Most typical use case is the need to skip StackTrace and TargetSite. Serilog is already reporting them so you may want Serilog.Exceptions to skip them to save space and processing time. To do that you just need to modify a line in configuration:

.Enrich.WithExceptionDetails(new DestructuringOptionsBuilder().WithFilter(someFilter));

Filtering for other scenarios is also supported:

  • Use WithIgnoreStackTraceAndTargetSiteExceptionFilter if you need to filter some other set of named properties
  • Implement custom IExceptionPropertyFilter if you need some different filtering logic
  • Use CompositeExceptionPropertyFilter to combine multiple filters

Continuous Integration

Name Operating System Status History
Azure Pipelines Ubuntu Azure Pipelines Ubuntu Build Status
Azure Pipelines Mac Azure Pipelines Mac Build Status
Azure Pipelines Windows Azure Pipelines Windows Build Status
Azure Pipelines Overall Azure Pipelines Overall Build Status Azure DevOps Build History
GitHub Actions Ubuntu, Mac & Windows GitHub Actions Status GitHub Actions Build History
AppVeyor Ubuntu & Windows AppVeyor Build Status AppVeyor Build History

Contributions and Thanks

Please view the contributing guide for more information.

  • 304NotModified - Added Markdown syntax highlighting.
  • joelweiss - Added Entity Framework Core destructurers.
  • krajek & JeroenDragt - For adding filters to help ignore exception properties you don't want logged.
  • krajek - For helping with cyclic dependencies when using the reflection destructurer.
  • mraming - For logging properties that throw exceptions.
  • optical - For a huge VS 2017 upgrade PR.
  • Jérémie Bertrand - For making Serilog.Exceptions compatible with Mono.
  • krajek - For writing some much needed unit tests.
  • .NETFramework 4.5
    • Serilog (>= 2.5.0)
  • .NETStandard 1.3
    • NETStandard.Library (>= 1.6.1)
    • Serilog (>= 2.5.0)
    • System.Reflection.TypeExtensions (>= 4.4.0)
  • .NETStandard 1.6
    • NETStandard.Library (>= 1.6.1)
    • Serilog (>= 2.5.0)
    • System.Reflection.TypeExtensions (>= 4.4.0)
  • .NETFramework 4.5: 4.5.0.0
  • .NETStandard 1.3: 1.3.0.0
  • .NETStandard 1.6: 1.6.0.0

Owners

Muhammad Rehan Saeed

Authors

Muhammad Rehan Saeed (RehanSaeed.com)

Project URL

https://github.com/RehanSaeed/Serilog.Exceptions

License

MIT

Tags

Serilog Exception Log Logging Detail Details

Info

141 total downloads
2 downloads for version 4.0.0-beta-0160
Download (37.07 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
5.1.0-beta-0213 74.81 KB Mon, 24 Dec 2018 15:07:12 GMT 5
5.1.0-beta-0212 74.81 KB Sun, 23 Dec 2018 13:07:31 GMT 6
5.0.0 74.72 KB Thu, 27 Dec 2018 14:24:43 GMT 5
5.0.0-beta-0220 74.82 KB Thu, 31 Jan 2019 11:58:43 GMT 5
5.0.0-beta-0217 74.81 KB Mon, 14 Jan 2019 10:01:36 GMT 4
5.0.0-beta-0214 74.81 KB Mon, 24 Dec 2018 15:18:36 GMT 6
5.0.0-beta-0210 56.97 KB Sun, 23 Dec 2018 11:29:12 GMT 4
5.0.0-beta-0209 56.96 KB Mon, 17 Dec 2018 06:49:04 GMT 4
5.0.0-beta-0205 56.96 KB Sat, 24 Nov 2018 21:56:33 GMT 4
5.0.0-beta-0204 56.96 KB Sat, 24 Nov 2018 21:54:14 GMT 4
5.0.0-beta-0201 56.96 KB Mon, 19 Nov 2018 08:57:28 GMT 4
5.0.0-beta-0199 56.96 KB Wed, 14 Nov 2018 19:52:12 GMT 4
5.0.0-beta-0198 48.77 KB Fri, 09 Nov 2018 08:42:55 GMT 3
5.0.0-beta-0196 48.76 KB Mon, 05 Nov 2018 09:48:13 GMT 3
5.0.0-beta-0195 48.76 KB Mon, 05 Nov 2018 09:36:45 GMT 3
5.0.0-beta-0194 48.76 KB Mon, 05 Nov 2018 09:28:53 GMT 4
5.0.0-beta-0193 48.77 KB Mon, 05 Nov 2018 09:23:17 GMT 4
5.0.0-beta-0192 48.77 KB Mon, 05 Nov 2018 09:16:04 GMT 4
5.0.0-beta-0191 48.77 KB Mon, 05 Nov 2018 08:49:15 GMT 3
4.1.0 37.27 KB Tue, 01 May 2018 08:22:07 GMT 4
4.1.0-beta-0171 37.5 KB Thu, 17 May 2018 14:08:03 GMT 4
4.1.0-beta-0165 37.34 KB Fri, 27 Apr 2018 20:02:25 GMT 4
4.1.0-beta-0163 37.35 KB Tue, 27 Mar 2018 18:41:08 GMT 3
4.0.0 33.21 KB Sun, 11 Feb 2018 07:44:01 GMT 5
4.0.0-beta-0161 37.32 KB Tue, 27 Mar 2018 18:31:30 GMT 4
4.0.0-beta-0160 37.07 KB Mon, 19 Feb 2018 14:14:08 GMT 2
4.0.0-beta-0159 37.17 KB Mon, 19 Feb 2018 09:20:15 GMT 3
4.0.0-beta-0158 34.88 KB Thu, 15 Feb 2018 08:30:32 GMT 1
4.0.0-beta-0157 33.31 KB Mon, 12 Feb 2018 13:44:41 GMT 1
4.0.0-beta-0156 33.29 KB Sun, 11 Feb 2018 16:17:16 GMT 1
4.0.0-beta-0154 33.29 KB Sun, 11 Feb 2018 07:34:39 GMT 2
3.0.0 33.24 KB Fri, 09 Feb 2018 21:18:24 GMT 2
3.0.0-beta-0152 33.32 KB Fri, 09 Feb 2018 20:45:55 GMT 1
3.0.0-beta-0151 33.33 KB Fri, 09 Feb 2018 20:26:10 GMT 2
3.0.0-beta-0150 33.31 KB Fri, 09 Feb 2018 20:20:37 GMT 2
3.0.0-beta-0149 33.33 KB Fri, 09 Feb 2018 19:18:59 GMT 1
3.0.0-beta-0148 33.33 KB Fri, 09 Feb 2018 15:30:45 GMT 1
3.0.0-beta-0147 33.9 KB Tue, 06 Feb 2018 16:26:31 GMT 1
3.0.0-beta-0146 31.59 KB Sat, 03 Feb 2018 20:51:57 GMT 1
3.0.0-beta-0145 31.58 KB Fri, 02 Feb 2018 15:04:22 GMT 0
3.0.0-beta-0143 31.59 KB Sat, 27 Jan 2018 06:32:24 GMT 0
3.0.0-beta-0142 31.45 KB Thu, 25 Jan 2018 20:20:06 GMT 0
3.0.0-beta-0141 30.65 KB Mon, 22 Jan 2018 11:30:43 GMT 1
3.0.0-beta-0140 30.66 KB Mon, 22 Jan 2018 10:51:51 GMT 1
3.0.0-beta-0139 30.67 KB Mon, 22 Jan 2018 10:49:04 GMT 0
3.0.0-beta-0138 30.67 KB Mon, 22 Jan 2018 10:47:01 GMT 2
3.0.0-beta-0130 28.06 KB Fri, 17 Nov 2017 09:14:24 GMT 1
2.5.0 28.79 KB Mon, 21 Aug 2017 15:17:54 GMT 2
2.5.0-beta-0129 28.33 KB Thu, 16 Nov 2017 09:44:55 GMT 0
2.5.0-beta-0128 29.09 KB Tue, 10 Oct 2017 08:49:09 GMT 0
2.5.0-beta-0127 28.88 KB Sat, 07 Oct 2017 07:10:37 GMT 1
2.5.0-beta-0126 28.88 KB Tue, 26 Sep 2017 15:51:42 GMT 0
2.5.0-beta-0125 28.88 KB Tue, 26 Sep 2017 11:42:18 GMT 1
2.5.0-beta-0124 28.87 KB Tue, 26 Sep 2017 11:37:12 GMT 0
2.5.0-beta-0123 28.87 KB Tue, 26 Sep 2017 11:33:39 GMT 1
2.4.1 30.35 KB Sat, 13 May 2017 05:33:55 GMT 2
2.4.0 29.06 KB Tue, 07 Feb 2017 16:38:21 GMT 3
2.3.0 29.06 KB Wed, 01 Feb 2017 13:39:12 GMT 0
2.2.1 28.38 KB Mon, 23 Jan 2017 14:31:48 GMT 1
2.2.0 25.51 KB Wed, 07 Sep 2016 10:53:26 GMT 1
2.0.1 25.48 KB Wed, 07 Sep 2016 10:33:14 GMT 0