在代码,xml和json中有用于“全局”覆盖MinimumLevel的文档。
但是我可以使用appsettings.json
覆盖特定的接收器吗?例如,我想以警告级别登录类MyClass
,但仅登录控制台接收器。
答案 0 :(得分:2)
只需在特定接收器的配置部分中使用 restrictedToMinimumLevel 参数:
{
/* .. */
"Serilog": {
"MinimumLevel": {
"Default": "Verbose", /* <-- set the default level to the LOWEST applicable for your app */
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.SystemConsoleTheme::Literate, Serilog.Sinks.Console",
"restrictedToMinimumLevel": "Information" /* <-- set the minimum level for specific sink */
}
},
{
"Name": "File",
"Args": {
/* .. */
"restrictedToMinimumLevel": "Warning" /* <-- set the minimum level for specific sink */
}
},
{
"Name": "Udp",
"Args": {
/* .. */
"restrictedToMinimumLevel": "Warning" /* <-- set the minimum level for specific sink */
}
}
],
/* .. */
}
}
答案 1 :(得分:1)
我也对这个问题感兴趣。我找不到解决方案。显然这不能正确完成,结果可能是这样的:
列表项
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var logger = new LoggerConfiguration().ReadFrom.ConfigurationSection(_configuration.GetSection("Serilog"))
.ReadFrom.ConfigurationSection(_configuration.GetSection("SerilogLow")).CreateLogger();
services.AddSingleton<ILogger>(logger);
}
HTH