How to change Serilog LogLevel at runtime for injected instance
Instead of setting the minimum log level in the Elasticsearch options, would you be able to set it at the Logger level? If you're able to instead do something like
Log.Logger = new LoggerConfiguration() .MinimumLevel.Error() .Enrich.FromLogContext() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(Configuration.GetSection("ElasticSearchURL").Value)) { AutoRegisterTemplate = true }).CreateLogger();
then you'd be able to use a LoggingLevelSwitch to control the minimum level, like this:
var levelSwitch = new LoggingLevelSwitch();levelSwitch.MinimumLevel = Serilog.Events.LogLevelEvent.Error;Log.Logger = new LoggerConfiguration() .MinimumLevel.ControlledBy(levelSwitch) .Enrich.FromLogContext() .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(Configuration.GetSection("ElasticSearchURL").Value)) { AutoRegisterTemplate = true }).CreateLogger();
You'd need to have something like a singleton for your instance of the LoggingLevelSwitch to inject along with your ILogger.