我有一个Azure Web作业(.NET Core 2.2
),在启动时像这样从配置中读取一些设置:
var builder = new HostBuilder()
.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
.ConfigureWebJobs()
.ConfigureAppConfiguration((hostContext, configApp) =>
{
configApp.AddEnvironmentVariables();
configApp.AddJsonFile("appsettings.json", optional: false);
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
var instrumentationKey = hostingContext.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
if (!string.IsNullOrEmpty(instrumentationKey))
{
Console.Writeline(instrumentationKey); // <- this always outputs key from appsettings.json, not from Azure Settings
logging.AddApplicationInsights(instrumentationKey);
}
})
.UseConsoleLifetime();
如您所见,appsettings.json
文件应该有一个APPINSIGHTS_INSTRUMENTATIONKEY
键,并且在开发环境中已经成功读取它。
现在,在生产环境中,我想通过在Azure应用程序设置Web界面中添加具有相同键的设置来覆盖此APPINSIGHTS_INSTRUMENTATIONKEY
键。
然而,当我将我的Web作业部署到Azure时,它仍然具有来自appsettings.json
的旧App Insights密钥。为了强制我的Web作业使用来自Azure Application设置的覆盖密钥,我必须删除appsettings.json
中的App Insights密钥。
是否有一种方法让我的Web作业在不必删除appsettings.json
中的键的情况下使用Azure应用程序设置?