serilog-exceptions - Serilog.Exceptions 2.2.1

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

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

Copy to clipboard

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

Copy to clipboard

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

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

nuget Serilog.Exceptions  ~> 2.2.1
Copy to clipboard

> choco install Serilog.Exceptions --version 2.2.1 --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.2.1" -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.2.1.

  • .NETFramework 4.5
    • Serilog (>= 2.2.1)
  • .NETStandard 1.3
    • NETStandard.Library (>= 1.6.0)
    • Serilog (>= 2.2.1)
    • System.Data.SqlClient (>= 4.1.0)
    • System.Reflection.TypeExtensions (>= 4.1.0)
  • .NETStandard 1.6
    • NETStandard.Library (>= 1.6.0)
    • Serilog (>= 2.2.1)
    • System.Data.SqlClient (>= 4.1.0)
    • System.Reflection.TypeExtensions (>= 4.1.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/netstandard1.3/serilog.exceptions.dll 02fde66dca4e4158a483725e3972102d1
/lib/netstandard1.6/serilog.exceptions.dll 07b07b58d27b4a5c8ef28f51ffa2e4ee1
/lib/net45/Serilog.Exceptions.dll 0F7B4E55270C40F5AFBBCCE44A83E54C1
/lib/net45/serilog.exceptions.dll 19d17686ac00487b8ae07fc9087fa6ad1
/lib/net45/Serilog.Exceptions.dll 1DF574DF7C054ECF9806A02CCCBD299A1
/lib/netstandard1.6/Serilog.Exceptions.dll 1E40E27FAC554116B1ECF5FEA452AC341
/lib/netstandard1.6/serilog.exceptions.dll 23ddfb14d4ed43129a9f59c615fcb8621
/lib/net45/serilog.exceptions.dll 23e0d9ba53e74042b842f0d7bf47158e1
/lib/netstandard1.6/Serilog.Exceptions.dll 2FB309BDE1344F9D8BCA4F46FC5A5BFC1
/lib/netstandard1.6/serilog.exceptions.dll 2fd9556433444395a6ac112b4bce7fd01
/lib/netstandard1.3/Serilog.Exceptions.dll 42A94127AB8F463F870924E080C701F31
/lib/netstandard1.6/Serilog.Exceptions.dll 43914987590247F2BA78B17188C29DB81
/lib/netstandard1.3/serilog.exceptions.dll 4b8bb6d7128148d5abda8d2023cf91a71
/lib/netstandard1.6/Serilog.Exceptions.dll 4BAE444BB77A40438FAA46203BCFEB4F1
/lib/netstandard1.6/Serilog.Exceptions.dll 4F4F087322E44BB9A4D57B57FC98D3BA1
/lib/netstandard1.3/Serilog.Exceptions.dll 52C8C6A114494798AEB3108768DDC0941
/lib/netstandard1.3/serilog.exceptions.dll 54ce3b72c62f456eafc6dcdb9a912efc1
/lib/netstandard1.3/serilog.exceptions.dll 56fe8413436e402b8ae950c4e6389df81
/lib/netstandard1.3/Serilog.Exceptions.dll 5B2B656031B642259DB682F67C5B2F851
/lib/netstandard1.6/Serilog.Exceptions.dll 5F41CC56633445828CB7D7994A22AB8F1
/lib/net45/Serilog.Exceptions.dll 6333DC67815B42A19D0020690E6047481
/lib/net45/serilog.exceptions.dll 6a96f723f4af48c09d10c5c615b848d41
/lib/netstandard1.6/Serilog.Exceptions.dll 7AFB0D6670094AABB677BB5E1608170A1
/lib/net45/serilog.exceptions.dll 7b5ae6baf6e344e3aca047251e80c5241
/lib/netstandard1.3/serilog.exceptions.dll 7cca443467b64ea1adf1ee4ec8f8b9da1
/lib/net45/Serilog.Exceptions.dll 7E1772D6E5F44D1C994BF2B3BC5BB7041
/lib/net45/Serilog.Exceptions.dll 83433097ACE14C74922771115FA403DC1
/lib/netstandard1.6/Serilog.Exceptions.dll 8D0B45B738434D6185F6BFE6D05AB6DA1
/lib/netstandard1.3/Serilog.Exceptions.dll 8FAF8B53C3C1453CA9664403D6F55B8F1
/lib/net45/Serilog.Exceptions.dll 8FCBA4C227794A519BCBD73BCD5754EF1
/lib/netstandard1.3/Serilog.Exceptions.dll 90C486BA66864104BAB1EC0165060EB31
/lib/netstandard1.3/Serilog.Exceptions.dll 9794EFA1E3C64561AF160371877418CC1
/lib/netstandard1.6/serilog.exceptions.dll 9a92c87c67bc4612841ae9e85ba5f9151
/lib/netstandard1.3/Serilog.Exceptions.dll 9B5FFB80CF804D639E6CA23A1A32D9B21
/lib/netstandard1.3/Serilog.Exceptions.dll A794F33F233A4486A470F52049BAA2191
/lib/netstandard1.3/Serilog.Exceptions.dll AADC0E3F3FD84F39A440E1AFE60CE3CD1
/lib/net45/Serilog.Exceptions.dll AADD691F7FA744C4B50612F2FAD958901
/lib/netstandard1.3/Serilog.Exceptions.dll AAF2ECD3ADE54EF08D2073A0B62952661
/lib/netstandard1.6/Serilog.Exceptions.dll AE4EABDF5C3A40C795EDFF6259774E3D1
/lib/net45/Serilog.Exceptions.dll B5F3D2F49582407CB1CC0F23013DAD471
/lib/netstandard1.6/Serilog.Exceptions.dll B874B666DF5C4581B5D9BAA0887721381
/lib/net45/Serilog.Exceptions.dll BE4564FE2F2048B0A9BE4021159B2E821
/lib/netstandard1.6/serilog.exceptions.dll c68d5731efd14e0a9661dde09cb5a4a11
/lib/netstandard1.3/Serilog.Exceptions.dll C6A619E71BFF407C811A4D6CD6FED5EA1
/lib/netstandard1.6/Serilog.Exceptions.dll CCD24BF8FFB64BD18A42BB3AEBFC067A1
/lib/net45/serilog.exceptions.dll cd716fd92ee44889b3166ef07ec486a61
/lib/netstandard1.6/Serilog.Exceptions.dll D3B6B170E305483996D30A130F694D851
/lib/net45/Serilog.Exceptions.dll D4B56A4D8DD149D78EFBE8A78995D9D31
/lib/net45/Serilog.Exceptions.dll D78F9A6A57204FA4A5548F5679CBFC1D1
/lib/netstandard1.3/Serilog.Exceptions.dll D847E983CFE8412FBB764DA9A3E4F3781
/lib/netstandard1.3/Serilog.Exceptions.dll E1E88224BD92472B943AC83FDDF8AF1D1
/lib/net45/Serilog.Exceptions.dll E74746014D824A90A83FACAAC7E780311
/lib/net45/Serilog.Exceptions.dll EBDB866A9F094F54B62473DCACB5C7F61
/lib/netstandard1.6/Serilog.Exceptions.dll ED3CCE7D68F84852B982858B37067E111

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
1 downloads for version 2.2.1
Download (28.38 KB)
Download legacy symbols (57.62 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