Serilog.Exceptions icon indicating copy to clipboard operation
Serilog.Exceptions copied to clipboard

Support non-default configuration with Serilog.Settings.Configuration

Open ChristofferGersen opened this issue 1 year ago • 2 comments

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 https://github.com/serilog/serilog-settings-configuration/pull/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
      }
    }
  ]
}

ChristofferGersen avatar Dec 21 '23 15:12 ChristofferGersen

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 😢

jeremy-allocate avatar May 03 '24 18:05 jeremy-allocate

@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.

TsengSR avatar Aug 08 '24 08:08 TsengSR