106

TL;DR

In an ASP.NET Core app, I have an appsettings.json config file which uses a JSON array to configure a collection of settings.

How do I override a setting of one of the array objects using environment variables?

Background

I'm using Serilog in an ASP.NET Core application and using the Serilog.Settings.Configuration, to allow it to be configured using appsettings.json.

The configuration is like this:

{
  "Serilog": {
    "Using":  ["Serilog.Sinks.Literate"],
    "MinimumLevel": "Debug",
    "WriteTo": [
      { "Name": "File", "Args": { "path": "%TEMP%\\Logs\\serilog-configuration-sample.txt" } }
    ],
    "Enrich": ["FromLogContext", "WithMachineName", "WithThreadId"],
    "Properties": {
        "Application": "Sample"
    }
  }
}

When deployed, I want to override some of the settings, e.g. the MinimumLevel, and the path to the log file. My preferred option is to do this via environment variables as I'm deploying to an Azure App Service, so I'll use the App settings through the Azure management portal (these are realised as environment variables).

I can easily set the MinimumLevel by adding an environment variable with the name: Serilog:MinimumLevel and the application name Serilog:Properties:Application.

What is the format for specifying a setting with an array?

James Skimming
  • 4,991
  • 4
  • 26
  • 32
  • 4
    I had the same problem - I could not stand the magic of names, colons and array indexes (Serilog:WriteTo:0:Args:path) and couple of months ago I wrote a new configuration provider to make this kind of configuration easier - https://github.com/dsbenghe/NotMicrosoft.Configuration – Dumitru Jun 29 '17 at 09:12
  • @Dumitru "Create appsettings.envname.json and repeat the WHOLE!!! Serilog json block again with the new values for MinimumLevel and pathFormat - this is probably the worst thing you can do" - this is not true. You just need to specify settings that change. – Piotr Perak Mar 04 '19 at 14:06

1 Answers1

158

After looking at the configuration in the debugger I found the answer.

  • Serilog__WriteTo__0__Args__path (All platforms)
  • Serilog:WriteTo:0:Args:path (Windows)
  • Serilog--WriteTo--0--Args--path (sourced From Azure Key Vault)

Note: The Configuration in ASP.NET Core documentation now covers this.

So I need to use the array index (zero-based) as if it were a name.

Here is the screenshot of the debugger, also (thanks to Victor Hurdugaci in the comments), the unit tests are a good place to look for examples.

configuration in the debugger

James Skimming
  • 4,991
  • 4
  • 26
  • 32
  • 8
    The tests are another great place to look for examples: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Extensions.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs – Victor Hurdugaci Jun 06 '16 at 16:32
  • 6
    @VictorHurdugaci Link is dead. Here's an updated one: https://github.com/aspnet/Configuration/blob/6cc477ed493e5b0da3c8f609488d667dc65a5576/test/Config.Binder.Test/ConfigurationCollectionBindingTests.cs – Jaanus Varus Nov 02 '17 at 13:33
  • @jaanus-varus Maybe, but the one in the answer is not, as I used a link based on the release tag. – James Skimming Nov 03 '17 at 08:52
  • 1
    @JamesSkimming Ah right, I only checked the link in the comment :) – Jaanus Varus Nov 03 '17 at 10:12
  • 6
    If you use this environment variable in a unix system, you have to replace the `:` with `__`: `Serilog__WriteTo__0__Args__path` – halllo Oct 24 '19 at 14:49
  • 4
    The now documentation suggests always using the __ form over the : form, even on Windows. – Nate Zaugg Sep 02 '20 at 20:27
  • Good point @nate-zaugg, I've swapped the answers to make the double underscore prominent. – James Skimming Sep 05 '20 at 08:44
  • is there any way to fully replace array with a brand new array? or it is possible only to overwrite values and/or extended the existing one? – Jacob Jun 30 '21 at 06:54
  • 1
    @Jacob, you should ask that as a new question. It's outside the scope of this question and, there may be several answers which could be worth expanding upon. – James Skimming Jul 07 '21 at 10:28