There is documentation for "globally" overriding MinimumLevel in code, xml and json.
But can I override for a specific sink, using appsettings.json
? For example, I want to log at warning level for class MyClass
, but only for the console sink.
Just use the restrictedToMinimumLevel param in the config section of the specific sink:
{
/* .. */
"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 */
}
}
],
/* .. */
}
}
I am interested in this question too. I can't find a solution. Apparently this cannot be done on correct.It can turn out like this:
List item
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With