在代码,xml和json中有“全局”覆盖MinimumLevel的文档。
但是我可以使用appsettings.json
覆盖特定的接收器吗?例如,我想记录类MyClass
的警告级别,但仅限于控制台接收器。
我也对这个问题感兴趣。我找不到解决方案。显然这不能在正确的情况下完成。它可以这样:
项目清单
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
只需在特定接收器的config部分中使用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 */
}
}
],
/* .. */
}
}