是否可以从环境变量设置 Serilog 最低日志级别?
如果我尝试像这样配置它
"Serilog": {
"MinimumLevel": "%LOG_LEVEL%",
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [v{SourceSystemInformationalVersion}] {Message}{NewLine}{Exception}",
"pathFormat": "%LOG_FOLDER%/sds-osdr-domain-saga-host-{Date}.log",
"retainedFileCountLimit": 5
}
}
]
}
它返回错误
The value %LOG_LEVEL% is not a valid Serilog level.
是否可以以某种方式从环境变量传播日志级别?
最佳答案
我想你是在问 configuration by environment这不是特定于 serilog。
如果LOG_LEVEL
与特定环境(开发、登台或生产)固定,您可以设置每个 LOG_LEVEL
在 appsettings.<EnvironmentName>.json
,并像这样设置配置:
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.Build();
如果需要配置LOG_LEVEL
从 docker-compose 文件或 kubernetes 部署文件中的环境变量,然后您可以通过调用 AddEnvironmentVariables
从环境变量中读取值:var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false)
.AddEnvironmentVariables()
.Build();
并设置环境Serilog:MinimumLevel
在 windows 或 ``Serilog__MinimumLevel` 在 linux 和 mac 中。
关于.net-core - 从环境变量设置 Serilog 最低级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49351130/