serilog-exceptions - Serilog.Exceptions 2.3.0

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

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

Copy to clipboard

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

Copy to clipboard

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

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

nuget Serilog.Exceptions  ~> 2.3.0
Copy to clipboard

> choco install Serilog.Exceptions --version 2.3.0 --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 "2.3.0" -Repository "serilog-exceptions" 
Copy to clipboard

Browse the sources in this package using Visual Studio or WinDbg by configuring the following legacy symbol server URL: https://www.myget.org/F/serilog-exceptions/symbols/


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.

Updated to target Serilog 2.3.0.

  • .NETFramework 4.5
    • Serilog (>= 2.3.0)
  • .NETStandard 1.3
    • NETStandard.Library (>= 1.6.0)
    • Serilog (>= 2.3.0)
    • System.Data.SqlClient (>= 4.3.0)
    • System.Reflection.TypeExtensions (>= 4.3.0)
  • .NETStandard 1.6
    • NETStandard.Library (>= 1.6.0)
    • Serilog (>= 2.3.0)
    • System.Data.SqlClient (>= 4.3.0)
    • System.Reflection.TypeExtensions (>= 4.3.0)
  • .NETFramework 4.5: 4.5.0.0
  • .NETStandard 1.3: 1.3.0.0
  • .NETStandard 1.6: 1.6.0.0

                        
Assembly Assembly hash Match
/lib/net45/serilog.exceptions.dll 03e9ce5689ab44c28c4d9b6bea377ea21
/lib/net45/serilog.exceptions.dll 0446019ed32d4d0799de3305fbaed6191
/lib/netstandard1.3/serilog.exceptions.dll 0895ac7b95bd452d83b2e2f16861913a1
/lib/netstandard1.6/serilog.exceptions.dll 0eeeee8eb8a44e5eaf74e4e750924d871
/lib/net45/serilog.exceptions.dll 1d59e521a6e545b2a7d385ed5f81f7851
/lib/netstandard1.6/serilog.exceptions.dll 259929b3816a4564b93ee331fdbd1d2d1
/lib/netstandard1.6/serilog.exceptions.dll 410d117acbd64d2bad13858890a0e86c1
/lib/netstandard1.6/serilog.exceptions.dll 47d8d31131c74e4d8407e252dd067de01
/lib/netstandard1.3/serilog.exceptions.dll 4db6d115c8bd4491ba4fb3fbc025bb941
/lib/net45/serilog.exceptions.dll 8fc3fddfce214651b46070ae3944a17e1
/lib/netstandard1.3/serilog.exceptions.dll a1c336f527fb41aea0350d2efd4235911
/lib/net45/serilog.exceptions.dll a77c6890377e47cfb813101058072c7b1
/lib/netstandard1.6/serilog.exceptions.dll c12c852693244860b35a4a93000aa8641
/lib/net45/serilog.exceptions.dll c21e55a042e243ff9503beb6e58752961
/lib/netstandard1.3/serilog.exceptions.dll c2faa43e42c64beaa1e9294d2955d7f31
/lib/net45/serilog.exceptions.dll c518edc743c04137bcc4a80178b69e261
/lib/netstandard1.6/serilog.exceptions.dll c8d3b4a077934008ba1a9bd96c2cc7ba1
/lib/net45/serilog.exceptions.dll d19caad2323e4c9d879b89133a3d92ce1
/lib/netstandard1.6/serilog.exceptions.dll d6b5be821f9d4f2583c967346983716c1
/lib/netstandard1.6/serilog.exceptions.dll dcf492b1042049c7bb80c5a49a696c951
/lib/netstandard1.3/serilog.exceptions.dll eb31368267b04ce8bee2629c5af3046c1
/lib/netstandard1.3/serilog.exceptions.dll f2e5b573a41a45419ebf78b3ea5787601
/lib/netstandard1.3/serilog.exceptions.dll f513a40c469e40159a165d3f7962d4751
/lib/netstandard1.3/serilog.exceptions.dll f8a0fd24676a447194d66a323c7fa08d1

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
0 downloads for version 2.3.0
Download (29.06 KB)
Download legacy symbols (58.58 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