How to override an ASP.NET Core configuration array setting using environment variables

只谈情不闲聊 提交于 2019-11-27 05:42:42

问题


LT;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?


回答1:


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

Serilog:WriteTo:0:Args:path

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

Below the screen shot of the debugger in case it helps, also (thanks to Victor Hurdugaci in the comments), the unit tests are a good place to look for examples.



来源:https://stackoverflow.com/questions/37657320/how-to-override-an-asp-net-core-configuration-array-setting-using-environment-va

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!