I am trying to implement logging in ASP.Net Core 8 application with serilog and the new Elastic.Serilog.Sinks (version 8.11.1) library and I am stuck with setting minimum log level. Here is the problematic part of my simple sample application:
var builder = WebApplication
.CreateBuilder(args);
builder.Host.UseSerilog((ctx, lc) => lc
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.Elasticsearch(new[] { new Uri("http://localhost:9200") }, opts =>
{
opts.MinimumLevel = Serilog.Events.LogEventLevel.Error;
opts.BootstrapMethod = BootstrapMethod.Failure;
opts.LevelSwitch = SwitchProvider.ElasticSwitch;
opts.DataStream = new DataStreamName("logs", "web-api-example");
}));
My SwitchProvider:
public static class SwitchProvider
{
public static LoggingLevelSwitch ElasticSwitch = new LoggingLevelSwitch();
}
I would expect that only Error level logs and higher are published to Elasticsearch. However every log with level Debug and higher is persisted (I have same logs in console and in Elasticsearch). Also I have tried to switch the level at runtime with the LoggingLevelSwitch
and nothing happens.
Am I missing something here?