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

Support non-default configuration with Serilog.Settings.Configuration #828

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ChristofferGersen
Copy link

This feature was suggested/requested in closed issues #58 and #300. I too like to configure Serilog using appsettings.json, and this pull request is based on the implementation I currently use. This pull requests adds an overload of WithExceptionDetails that builds a DestructuringOptionsBuilder based on the provided arguments.

I did not write test cases for this, since I could not come up with an easy way to do this.

The following example adds a single destructurer on top of the default ones.

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Exceptions" ],
  "MinimumLevel": "Information",
  "WriteTo": [ { "Name": "Console" } ],
  "Enrich": [
    {
      "Name": "WithExceptionDetails",
      "Args": {
        "destructurers": [ { "type": "Serilog.Exceptions.MsSqlServer.Destructurers.SqlExceptionDestructurer, Serilog.Exceptions.MsSqlServer" } ]
      }
    }
  ]
}

The following example adds an extra property filter. Note that this does not work at the moment, because Serilog.Settings.Configuration has problems with arrays inside constructor arguments. I already created a pull request for that at serilog/serilog-settings-configuration#405. When using the changes from that pull request the following will work.

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Exceptions" ],
  "MinimumLevel": "Information",
  "WriteTo": [ { "Name": "Console" } ],
  "Enrich": [
    {
      "Name": "WithExceptionDetails",
      "Args": {
        "filters": [
          {
            "type": "Serilog.Exceptions.Filters.IgnorePropertyByNameExceptionFilter, Serilog.Exceptions",
            "propertiesToIgnore": [ "Prop1", "Prop2" ]
          }
        ]
      }
    }
  ]
}

The following example specifies the default configuration explicitly. This is not very useful, except that it shows what all configuration options are.

"Serilog": {
  "Using": [ "Serilog.Sinks.Console", "Serilog.Exceptions" ],
  "MinimumLevel": "Information",
  "WriteTo": [ { "Name": "Console" } ],
  "Enrich": [
    {
      "Name": "WithExceptionDetails",
      "Args": {
        "defaultDestructurers": false,
        "destructurers": [
          { "type": "Serilog.Exceptions.Destructurers.ExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.ArgumentExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.ArgumentOutOfRangeExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.AggregateExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.RegexMatchTimeoutExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.ReflectionTypeLoadExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.OperationCanceledExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.TaskCanceledExceptionDestructurer, Serilog.Exceptions" },
          { "type": "Serilog.Exceptions.Destructurers.SocketExceptionDestructurer, Serilog.Exceptions" }
        ],
        "defaultFilters": false,
        "filters": [
          {
            "type": "Serilog.Exceptions.Filters.IgnorePropertyByNameExceptionFilter, Serilog.Exceptions",
            "propertiesToIgnore": [ "StackTrace" ]
          }
        ],
        "rootName": "ExceptionDetail",
        "destructuringDepth": 10,
        "disableReflectionBasedDestructurer": false
      }
    }
  ]
}

@jeremy-allocate
Copy link

Would love to see this get merged soon. We do all of our serilog config through appsettings and I need to use Serilg.Exceptions.EntityFramework to make sure the Entities collection on DbUpdateExceptions aren't serialized 😢

@TsengSR
Copy link

TsengSR commented Aug 8, 2024

@RehanSaeed How about getting this merged? The upstream serilog/serilog-settings-configuration#405 seems to be merged already and this one would be pretty useful as we configure most of Serilog via json to enable specific log features per installation/envionment.

@michael-freidgeim-webjet

@RehanSaeed any chance to merge it?
It will be very convenient for those who are using appSettings configuration.

@pinkfloydx33
Copy link

Would appreciate this change as well. Just spent a decent amount of time trying to figure out how to achieve this

@codymullins
Copy link

@RehanSaeed is anything blocking from merging this in, besides the build failure?

@TsengSR
Copy link

TsengSR commented Feb 5, 2025

@RehanSaeed is anything blocking from merging this in, besides the build failure?

Guess the maintainer lost all interest, as there have been no activity for the past 2 years from the maintainer. Which is a pity as this extension is used quite widely and the bug in question is kinda critical when you work with EF Core, where one needs to add custom destructurers to properly handle Excpetions from DbContext/DbSet/IQueryable objects and currently the only way to configure this is via code, not via json configuration file.

@JeremyCaron
Copy link

Sounds like a fork is in order.

@RehanSaeed
Copy link
Owner

Apologies guys, life got on top of me and I've been unable to keep up with this project.

@SimonCropp Do you have bandwidth to help here.

@SimonCropp SimonCropp added this to the 9.0.0 milestone Feb 13, 2025
@SimonCropp
Copy link
Collaborator

does anyone on this thread want to add tests for this change?

@ChristofferGersen
Copy link
Author

I added some test cases. I added dependencies to Microsoft.Extensions.Configuration.Json and Serilog.Settings.Configuration, which allows me to call the extension method just like you would using appsettings.json. These dependencies gave me compiler warnings for .Net 6 and 7, as in "System.Text.Json 9.0.2 doesn't support net7.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later". To fix this I downgraded both dependencies for .Net 6 and 7.

I used reflection to get to the destructuringOptions field inside ExceptionEnricher. Same for the filters field of CompositeExceptionPropertyFilter. I hope this is not a problem. Without reflection I would have to call ExceptionEnricher.Enrich and check the properties that were added, which would make the test cases a lot more complex.

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.

9 participants