如何使用环境变量覆盖ASP.NET Core配置数组设置 [英] How to override ASP.NET Core configuration array setting using environment variables

查看:185
本文介绍了如何使用环境变量覆盖ASP.NET Core配置数组设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

TL;DR

在ASP.NET Core应用程序中,我有一个 appsettings.json 配置文件,该文件使用JSON数组来配置设置的集合

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?

背景

我在ASP.NET Core应用程序中使用 Serilog ,并使用 Serilog.Settings.Configuration ,以允许使用<$ c进行配置$ c> appsettings.json 。

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.

配置如下:

{
  "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"
    }
  }
}

部署后,我想覆盖一些设置,例如 MinimumLevel ,以及日志文件的路径。我的首选方法是在部署到Azure App Service时通过环境变量来执行此操作,因此,我将通过Azure管理门户使用App设置(这些变量将实现为环境变量)。

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).

我可以通过添加名称为 Serilog:MinimumLevel 和应用程序名称<$ c $的环境变量来轻松设置 MinimumLevel c> Serilog:Properties:Application 。

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?

推荐答案

在调试器中查看配置后,我找到了答案。

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


  • Serilog__WriteTo__0__Args__path (所有平台)

  • Serilog:WriteTo:0:Args:path (适用于Windows)

  • Serilog--WriteTo--0--Args--path (源自Azure Key Vault)

  • 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.

以下是调试器的屏幕截图,(感谢评论中的Victor Hurdugaci ),单元测试是查找示例的好地方。

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.

这篇关于如何使用环境变量覆盖ASP.NET Core配置数组设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆