我在 azure 上有一个运行 .net core api 的 appservice。
在我的 appsettings.json 文件中,我有一个类似于以下内容的部分:
"Serilog": {
"LevelSwitches": { "$controlSwitch": "Information" },
"MinimumLevel": {
"ControlledBy": "$controlSwitch",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "LOGS\\log.json",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "https://MyLoggingServer",
"apiKey": "AAAAAAAAAAAAAAAAA",
"controlLevelSwitch": "$controlSwitch"
}
}
]}
Run Code Online (Sandbox Code Playgroud)
在 azure 门户的 azure appsetting 部分中,我不确定如何设置 apiKey,在其他更简单的设置中,我在 appsettings.json 中有另一个部分
"CustomSettings": {
"MySpecificSetting": "ABCDEFG",
}
Run Code Online (Sandbox Code Playgroud)
然后在 azure 门户中,我可以通过执行以下操作来设置设置
CustomSettings:MySpecificSetting
Run Code Online (Sandbox Code Playgroud)
但我不确定这种语法如何允许我访问 writeTo …
我们需要将配置提供程序添加到本机提供给 Azure Functions 的本机 IConfiguration。目前,我们正在使用以下代码将其完全替换为我们的自定义 Iconfiguration:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
...
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddAzureKeyVault(...)
.AddJsonFile("local.settings.json", true, true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddSingleton<IConfiguration>(configuration);
builder.Services.AddSingleton<IMyService, MyService>();
}
}
Run Code Online (Sandbox Code Playgroud)
一些上下文
MyService 在其构造函数中需要来自 KeyVault 提供程序的值以及 Application Insights 等其他实例。如果我们保留默认的 IConfiguration,则它没有 KeyVault 值。如果我们用工厂创建 MyService 实例,我们需要手动提供 App Insights 实例等。目前替换 IConfiguration 编译和函数运行。但它打破了其他默认行为,例如不从 host.json 获取配置(我们正在尝试配置队列触发器)。使用默认的 IConfiguration 可以正确读取 host.json 中的设置。