Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reconfigure Serilog without restarting the application?

In a long running process (such as a Windows service or an ASP.NET application) it’s sometimes desirable to temporarily increase the log level without stopping the application. NLog can monitor logging configuration files and re-read them each time they are modified.

https://github.com/nlog/NLog/wiki/Configuration-file#automatic-reconfiguration

Is this also possible with Serilog?

like image 680
Robin van der Knaap Avatar asked Aug 24 '14 23:08

Robin van der Knaap


People also ask

How do I turn off Serilog logging?

To suppress all logs, specify LogLevel. None. LogLevel. None has a value of 6, which is higher than LogLevel.

What is minimum level in Serilog?

The MinimumLevel configuration object provides for one of the log event levels to be specified as the minimum. In the example above, log events with level Debug and higher will be processed and ultimately written to the console. Verbose is the noisiest level, rarely (if ever) enabled for a production app.

What is Serilog Enricher?

Log Context enricher - Built in to Serilog, this enricher ensures any properties added to the Log Context are pushed into log events. Environment enrichers - Enrich logs with the machine or current user name.

What is Serilog sink?

Serilog is a newer logging framework for . NET. It was built with structured logging in mind. It makes it easy to record custom object properties and even output your logs to JSON. Note: You can actually check out our other tutorials for NLog and log4net to learn how to do structured logging with them also!


1 Answers

Use LoggingLevelSwitch for this:

// Set initial level
var levelSwitch = new LoggingLevelSwitch(LogEventLevel.Warning);

Log.Logger = new LoggerConfiguration()
  .MinimumLevel.ControlledBy(levelSwitch)
  .WriteTo.Console()
  .CreateLogger();

Log.Debug("This is not shown");

levelSwitch.MinimumLevel = LogEventLevel.Debug;

Log.Debug("This will now be shown, since the level has changed");

When the `levelSwitch.MinimumLevel is changed, the logger will pick up the new minimum level setting.

For Serilog 1.4.10 and earlier

Serilog doesn't bake this in as a first-class notion.

This can be emulated using a filter:

// Stored e.g. in a static field
volatile LogEventLevel minLevel;

Log.Logger = new LoggerConfiguration()
    .Filter.ByExcluding(evt => (int)evt.Level < (int)minLevel)
    .CreateLogger();

It'd be up to you to determine how minLevel gets modified as the app runs.

This approach isn't as efficient as setting the minimum level natively since the events will be generated in all cases, but the actual overhead shouldn't be huge.

Depending on the sinks you're using, an alternative is to simply create multiple loggers, and choose between them:

var minVerboseLogger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .CreateLogger();

var minWarningLogger = new LoggerConfiguration()
    .MinimumLevel.Warning()
    .CreateLogger();

// Somewhere else:
public ILogger Log
{
    get { return isVerbose ? minVerboseLogger : minWarningLogger; }
}

The second approach is preferable, but won't behave nicely if the two loggers need to share the same log file. If you need to write to a file in both cases, chain the higher-level logger to the lower one, e.g.:

var minWarningLogger = new LoggerConfiguration()
    .MinimumLevel.Warning()
    .WriteTo.Sink((ILogEventSink)minVerboseLogger)
    .CreateLogger();

Admittedly this is more complex than the NLog approach you linked; I'll give some thought as to how we might make it smoother.

like image 103
Nicholas Blumhardt Avatar answered Sep 29 '22 23:09

Nicholas Blumhardt