我正在编写一个 .net 控制台应用程序,但我在设置日志记录级别时遇到了问题。我已经使用“日志记录”部分创建了 appsettings 文件,但它不起作用 - 我将级别设置为 Error 并且仍然收到信息级别错误。我知道 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"
}
}
}
添加直接登录配置解决了我的问题:
.ConfigureLogging((context, config) =>
config.AddConfiguration(context.Configuration.GetSection("Logging")))
整个主要功能:
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();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句