Search code examples
asp.net-coreappsettingsserilog

Appending arrays in from appsettings.{Environment}.json to appsettings.json


This is very similar to this question except its about appending arrays between two different JSON files.

I have an ASP.NET Core application

I have the following in appsettings.Development.Json

"Serilog": {
  "WriteTo": [
    {
      "Name": "ApplicationInsightsTraces",
      "Args": { "instrumentationKey": "XXXXXXXX" }
    }
  ]
}

And in appsettings.json:

"Serilog": {
  // . . . Rest of Serilog configs  
  "WriteTo": [
    {
      "Name": "Console",
      "Args": {
      "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
      "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {RequestId}-{SourceContext} {$Scope:lj}: {Message:lj}{NewLine}{Exception}"
    },
    "restrictedToMinimumLevel": "Information"
  },
}

Because Keys overwrite other keys in Appsettings.json I end up with the Console sink being overridden. Is there a syntax to allow it to be appended?


Solution

  • The answer is to use WriteTo:1 and make it an object not an array in appsettings.Development.json like so:

    "Serilog": {
      "WriteTo:1": 
      {
        "Name": "ApplicationInsightsTraces",
        "Args": { "instrumentationKey": "d95066c9-0b17-4e0a-84d4-bb2a4f111016" }
      }
    }