serilog / serilog-settings-configuration

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

Arguments in sink (appsettings.json) is transferred to environment-specific sink (appsettings.development.json) #419

Closed stefanvincenthaug closed 4 months ago

stefanvincenthaug commented 4 months ago

I expected the array of sinks to be overwritten as a whole by the configuration in the environment-specific appsettings, but elements are being preserved.

In the following example, the argument "restrictedToMinimumLevel" is also applied to the console-sink specified in appsettings.development.

The argument is strictly not neccessary in this scenario, but is present to illustrate the issue. By removing the argument, the debug-statement is being output to the console as expected.

appsettings.json:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.ApplicationInsights" ],
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "ApplicationInsights",
        "Args": {
          "connectionString": null,
          "restrictedToMinimumLevel": "Information",
          "telemetryConverter": "Serilog.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}

appsettings.development.json:

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": "Debug",
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  }
}

program.cs:

using Microsoft.Extensions.Configuration;
using Serilog;

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .AddJsonFile($"appsettings.Development.json", true)
    .Build();

var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

logger.Debug("This is not written to console.");
logger.Information("This is written to console.");
nblumhardt commented 4 months ago

Hi! Unfortunately this is how arrays are handled by the underlying Microsoft.Extensions.Configuration system. We've explored a syntax that avoids arrays, in the past - may be nice to revisit at some point.

stefanvincenthaug commented 4 months ago

Thank you, you are correct of course. I've observed this numerous times previously, I'll adjust my mindset on how I think of the application settings.