serilog / serilog-settings-configuration

A Serilog configuration provider that reads from Microsoft.Extensions.Configuration
Apache License 2.0
458 stars 129 forks source link

Correct way to override formatters with environment-specific appsettings? #434

Closed denniskrq closed 2 months ago

denniskrq commented 2 months ago

I have the following WriteTo section configured in my main appsettings.json:

"WriteTo": [
      {
        // Logging to file for offline analysis
        "Name": "File",
        "Args": {
          "path": "./Logs/testService-.log",
          "formatter": { "type": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" },
          "rollingInterval": "Day",
          "retainedFileCountLimit": 7
        }
      },
      {
        // Rendered Compact JSON console logging for OpenSearch forwarder
        "Name": "Console",
        "Args": {
          "formatter": { "type": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact" }
        }
      }
    ]

I have the following WriteTo section configured in my appsettings.Development.json:

"WriteTo": [
      {
        // Templated plain text logging for readability
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}"
        }
      }
    ]

What I'm observing when I run my service from a local development build is that instead of the plain text template overriding the rendered compact JSON, both of them appears (the template text outputs before the compact JSON). What's the correct way to get this override working with appsettings.json? I would like to be able to centralize all of my settings in the json files rather than needing to do part of it in code

0xced commented 2 months ago

There's a section in the README that explains why this problem occurs and how to solve it: use named WriteTo section.

For you, it should look like this in your appsettings.json file:

{
  "WriteTo": {
    "FileSink": {
      "Name": "File",
      "Args": {
        "path": "./Logs/testService-.log",
        "formatter": {
          "type": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        },
        "rollingInterval": "Day",
        "retainedFileCountLimit": 7
      }
    },
    "ConsoleSink": {
      "Name": "Console",
      "Args": {
        "formatter": {
          "type": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    }
  }
}

And like this in your appsettings.Development.json file:

{
  "WriteTo": {
    "ConsoleSink": {
      "Name": "Console",
      "Args": {
        "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties}{NewLine}{Exception}"
      }
    }
  }
}