How to configure Serilog with different log levels for each namespace in my project? I want to do it via application.json config file
As you suggested in your configuration as well. It is possible to do so. Below is the sample configuration.
"serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Elasticsearch.ElasticsearchJsonFormatter,Serilog.Formatting.Elasticsearch"
}
}
]}
MinimumLevel config section dictates what should be default and overrides for specific namespaces.
- Default log level is Information.
- Classes in Microsoft namespace except Microsoft.Hosting.Lifetime should have minimum log level as Warning
As side note I registered my Serilog as
webBuilder.UseSerilog((ctx, lc) => lc.ReadFrom.Configuration(ctx.Configuration));
And formatter part is Trivial in context of the question but if you want to use it I refered Serilog.Sinks.ElasticSearch as extra package to use it.