Reputation: 356
I am trying to implement logging in an ASP.NET Core 8 application with Serilog (version 8.0.1) and the "new" Elastic.Serilog.Sinks
(version 8.11.1) library. I am stuck at setting minimum log level for the Elasticsearch sink.
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");
})
);
As you can see I set the global minimumLevel to Debug
and the Elasticsearch minimum level to Error
. However all logs, no matter the level (well, all logs with higher level than the global minimumLevel to be precise), are sent to the Elasticsearch - I can see Debug, Information etc. level logs in Elasticsearch (basically the logs in console and in Elasticsearch are the same).
I have also tried to switch the level for Elasticsearch sink at runtime via the
ElasticSwitch.MinimumLevel = Serilog.Events.LogEventLevel.Error;
through my basic SwitchProvider
which is here:
public static class SwitchProvider
{
public static LoggingLevelSwitch ElasticSwitch = new LoggingLevelSwitch();
}
but with no effect.
Am I missing something here?
Upvotes: 0
Views: 539
Reputation: 8811
You could try not using global configuration, but using sub loggers to configure console sink and elasticSearch sink.
builder.Host.UseSerilog((ctx, lc) => lc
.WriteTo.Logger(l => l
.MinimumLevel.Debug()
.WriteTo.Console())
.WriteTo.Logger(l => l
.MinimumLevel.Error()
.WriteTo.Elasticsearch(new[] { new Uri("http://localhost:9200") }, opts =>
{
opts.BootstrapMethod = BootstrapMethod.Failure;
opts.LevelSwitch = SwitchProvider.ElasticSwitch;
opts.DataStream = new DataStreamName("logs", "web-api-example");
})
));
Upvotes: 0