Tolyandre / serilog-throw-context-enricher

Captures LogContext of a thrown exception in order to enrich the subsequent exception logs with the origin context
MIT License
41 stars 2 forks source link

Add configuration example to readme #2

Closed MNF closed 1 year ago

MNF commented 3 years ago

It will be good to add to readme an example of configuration as an alternative to hardcoded new LoggerConfiguration() .Enrich.With()- something like

{ "Serilog": { "MinimumLevel": "Debug", "Using": [ "Serilog.ThrowContext" ], "Enrich": [ "ThrowContextEnricher"], "WriteTo": [ { "Name": "Console" } ] } }

I haven’t tried it yet. Will it work ?

Tolyandre commented 3 years ago

I suppose you use https://github.com/serilog/serilog-settings-configuration.

It looks like the configuration should be:

  "Serilog": {
    "MinimumLevel": "Debug",
    "Enrich": [
      "FromLogContext",
      {
        "Name": "With",
        "Args": { "enricher": "Serilog.ThrowContext.ThrowContextEnricher, Serilog.ThrowContext" }
      }
    ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    ]
  }

(I added FromLogContext and RenderedCompactJsonFormatter because without them we cannot observe context properties).

Unfortunately, this configuration throws System.MemberAccessException: 'Type initializer was not callable.'. I think this happens because ThrowContextEnricher has a static constructor and serilog-settings-configuration somehow invokes it instead of instance ctor.

julealgon commented 1 year ago

@Tolyandre I'm assuming this is now resolved? The related problem has since been fixed.

Tolyandre commented 1 year ago

Hey @julealgon, thanks for reaching out to me.

I have just tested and it works. I also added a sample csproj https://github.com/Tolyandre/serilog-throw-context-enricher/tree/master/Samples/FileConfigurationSample

So yes, I will close this