Serilog配置读取根据.NET 5中的Worker Service中的环境变量读取
根据环境变量,我对Serilog有不同的配置。它们写在appsettings.json and appsetting.development.json中。我的问题是在配置Logger时,即使在开发模式下,也不会从AppSettings.development.json读取。
我尝试过这样的配置,
.ConfigureAppConfiguration((w, c) =>
{
var env = w.HostingEnvironment;
if (env.EnvironmentName == "Development")
{
c.AddCustomJsonFile($"appsettings.{env.EnvironmentName}.json");
}
else
{
c.AddCustomJsonFile($"appsettings.json");
}
})
.ConfigureServices((hostContext, services) =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostContext.Configuration)
.CreateLogger();
}).UseSerilog();
而我的appsettings.json就是这样,
"Serilog": {
"MinimumLevel": {
"Defult": "Information",
"Override": {
"Micorsoft": "Warning",
"System": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341/"
}
}
]
在appsetting.development.json中,
"Serilog": {
"MinimumLevel": {
"Defult": "Debug",
"Override": {
"Micorsoft": "Warning",
"System": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341/"
}
}
]
我已经使用其他服务对其进行了测试,它使用了基于环境变量的正确appsettings,只有Serilog就是有问题。有人知道如何解决这个问题吗? 提前致谢。
I have different configurations for Serilog based on the Environment variable. And they are written in the appsettings.json and appsetting.Development.json. My problem is when configuring logger it does not read from the appsettings.Development.json even in Development mode.
I've tried to configure it like this,
.ConfigureAppConfiguration((w, c) =>
{
var env = w.HostingEnvironment;
if (env.EnvironmentName == "Development")
{
c.AddCustomJsonFile(quot;appsettings.{env.EnvironmentName}.json");
}
else
{
c.AddCustomJsonFile(quot;appsettings.json");
}
})
.ConfigureServices((hostContext, services) =>
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(hostContext.Configuration)
.CreateLogger();
}).UseSerilog();
And my appsettings.json is like this,
"Serilog": {
"MinimumLevel": {
"Defult": "Information",
"Override": {
"Micorsoft": "Warning",
"System": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341/"
}
}
]
And in appsetting.Development.json,
"Serilog": {
"MinimumLevel": {
"Defult": "Debug",
"Override": {
"Micorsoft": "Warning",
"System": "Information"
}
},
"WriteTo": [
{ "Name": "Console" },
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341/"
}
}
]
I've tested it with other services where it uses the correct appsettings based on the environment variable, Just Serilog is having problems with this. Does anybody know how to solve this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论