我正在编写一个 .net 控制台应用程序,我有一个设置日志级别的问题。我创建了带有“Logging”部分的 appsettings 文件,但它不起作用 - 我将级别设置为错误,但仍然得到信息级别的错误。
我知道 appsettings 正确加载,因为其余设置已正确绑定。
我的主类:
static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", true);
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true);
config.AddEnvironmentVariables();
if (args != null)
config.AddCommandLine(args);
})
.ConfigureServices((_, services) =>
services.AddMagic())
.RunConsoleAsync();
}
我的appsettings.json文件内容如下:
{
"Postgres": {
"Host": "localhost",
"DatabaseName": "db",
"Username": "admin",
"Password": "admin",
"MinLogLevel": "Error"
},
"Redis": {
"Host": "localhost:6379",
"Password": null
},
"Logging": {
"LogLevel": {
"Default": "Error"
}
}
}