I'm writing a .net console application, and I've a problem to set logging levels. I've created appsettings files with "Logging" section, but it doesn't work - I set the level to Error and still gets Info level errors. I know that appsettings are loading correctly because the rest of the settings are correctly binded.
My main class:
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();
}
and my appsettings.json:
{
"Postgres": {
"Host": "localhost",
"DatabaseName": "db",
"Username": "admin",
"Password": "admin",
"MinLogLevel": "Error"
},
"Redis": {
"Host": "localhost:6379",
"Password": null
},
"Logging": {
"LogLevel": {
"Default": "Error"
}
}
}
Adding a direct login configuration resolved my problem:
.ConfigureLogging((context, config) =>
config.AddConfiguration(context.Configuration.GetSection("Logging")))
Whole main function:
static async Task Main(string[] args)
{
await new HostBuilder()
.ConfigureAppConfiguration((context, config) =>
{
config.AddEnvironmentVariables();
config.AddJsonFile("appsettings.json", true, true);
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", true, true);
if (args != null)
config.AddCommandLine(args);
})
.ConfigureLogging((context, config) =>
config.AddConfiguration(context.Configuration.GetSection("Logging")))
.ConfigureServices(services =>
services.AddMagic())
.RunConsoleAsync();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.