【发布时间】:2022-01-06 18:26:09
【问题描述】:
谁能告诉我如何从 Blazor(核心 5)应用程序的 program.cs 文件中的 appsettings.json 文件中获取值。 基本上只需要正确写出“var seqUrl =”。我认为。 请和谢谢。
在我的 Program.cs 的 Main 方法中,我有
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile("appsettings.Development.json", true, true)
.Build();
var levelSwitch = new LoggingLevelSwitch();
var seqUrl = configuration.GetSection("SeriLog").GetSection("WriteTo").GetSection("Name:Seq").GetSection("Args").GetSection("serverUrl").Value;
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.MinimumLevel.ControlledBy(levelSwitch)
.WriteTo.Seq(seqUrl,
apiKey: "xxxxxxxxxxxx",
controlLevelSwitch: levelSwitch)
.CreateLogger();
我的 appsettings.json 看起来像这样。
"SeriLog": {
"Using": [
"Serilog.Sinks.File",
"Serilog.Sinks.Seq",
"Serilog.Sinks.Console"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"system": "Warning"
}
},
"Enrich": [ "WithMachineName", "WithEnvironmentUserName", "WithClientIp", "WithClientAgent", "WithEnvironmentName", "WithProcessId", "WithProcessName", "WithThreadId" ],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\Temp\\Logs\\JsonLog.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"rollingInterval": "Day"
}
},
{
"Name": "Seq",
"Args": {
"serverUrl": "http://xxxxxxxxxxx"
}
}
]
【问题讨论】:
-
为什么你的命令中有
.GetSection("Name:Seq")?没有这样的部分,右侧的外观不是“名称”的子项。 -
好问题...我知道这是错的,这就是为什么我问是否有人知道如何正确地写那行。
标签: c# configuration blazor settings