Reputation: 362
I would like to understand if it is possible to change the logging level from Debug to Error on the fly in the production envionment.
FOr instance in Nlog, there is a place in the config file, where i can set the logging granularity.
<level value="Info" />
This can be changed on the fly in the UAT environment to get Debug level errors and then after a while, i can change it back to INfo. THis is just a config change and no code change.
How do we handle a similar scenario with Serilog.
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.RollingFile("log-{Date}.txt")
.WriteTo.LiterateConsole(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
If i need Error logging alone then my config will look like
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Error()
.WriteTo.RollingFile("log-{Date}.txt")
.WriteTo.LiterateConsole(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
Is it possible to change the minimum level to Error on the fly without making a code change??
Upvotes: 3
Views: 1488
Reputation: 31832
Serilog has an XML app settings provider for App.config
/Web.config
, and a JSON configuration provider for .NET Core's configuration subsystem.
To do this with an App.config
/Web.config
file, first install the package:
Install-Package Serilog.Settings.AppSettings
Add keys to <appSettings>
in your config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="serilog:minimum-level" value="Debug" />
And read this at logger configuration time:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.WriteTo.RollingFile("log-{Date}.txt")
.WriteTo.LiterateConsole(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
You can add config entries to write to sinks, enrich with properties, etc. as well. See the documentation at the provider link above for the full set.
Upvotes: 4