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 1 commit 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

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.

5 participants