Serilog.Enrichers.Sensitive icon indicating copy to clipboard operation
Serilog.Enrichers.Sensitive copied to clipboard

Different configuration for different sink types

Open mikewop opened this issue 1 year ago • 3 comments

Is it possible to have different configurations for different types of sinks? E.g. mask different/less items in log entries going to a file sink vs one going to Azure Application Insights or NewRelic etc.

Or if that's not possible only apply the enricher to certain sink types?

mikewop avatar Nov 14 '23 22:11 mikewop

That's a good question and not something I've looked into before.

A quick search reveals this StackOverflow answer that you can use a sub-logger like so:

Log.Logger = new LoggerConfiguration()
    .WriteTo.File("app.log") // This doesn't have the sensitive data scrubbing
    .WriteTo.Logger(lc => lc
        .Enrich.WithSensitiveDataMasking()
        .WriteTo.ApplicationInsights() // This will get sensitive data scrubbing
    .CreateLogger();

which would give you a bunch of flexibility.

Would that help?

sandermvanvliet avatar Nov 23 '23 12:11 sandermvanvliet

Yes thank you, I had come across that too. But I am not sure if you can configure a sublogger using the appsettings.json file ? e.g. I'd like to configure this in the settings file

   "Using": [
      "Serilog.Enrichers.Sensitive"
    ],
    "Enrich": [
      {
        "Name": "WithSensitiveDataMasking",
        "Args": {
          "options": {
            "MaskProperties": [
              "UserName"
            ],
            "ExcludeProperties": [
              "EmailAddresses"
            ],
            "Mode": "Globally"
          }
        }
      }
    ]

mikewop avatar Dec 01 '23 23:12 mikewop

No idea if that's possible. The JSON configuration is fairly limited in this regard and configuring through code gives you much more options and granularity so I would recommend doing that instead.

sandermvanvliet avatar Jan 07 '24 19:01 sandermvanvliet