question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change logger/sink configuration at runtime?

See original GitHub issue

Is there a straightforward way to change the logger configuration (or sink configuration) at runtime?

I generally set up my main logger with a rolling file sink to print out critical errors (especially at startup) as this works in most scenarios. Independently of what type of application I have (service, website, console), I only need to ensure the file system is write-able and I’m good to go.

// dummy
var logfilepath = GetLogFilePath(Assembly.GetEntryAssembly());
Log.Logger = new LoggerConfiguration()
                        .WriteTo.RollingFile(logfilepath)
                        .CreateLogger();
Log.Debug("It works!");

However, I also would like to log into other sources that are more prone to fail at startup (e.g. databases when wrong connection strings are given). So what I would like to achieve is the following:

  1. Set up a simple, stable logging sink for first startup
  2. Set up e.g. database, log everything related to it
  3. If e.g. database is up and running, add database sink to serilog.

The only way I see to accomplish this is:

  1. Create a logger configuration
  2. Do stuff
  3. Create a new logger configuration and overwrite the existing one.

Dummy code:

var logfilepath = GetLogFilePath(Assembly.GetEntryAssembly());
Log.Logger = new LoggerConfiguration()
                        .WriteTo.RollingFile(logfilepath)
                        .CreateLogger();
Log.Debug("It works!");

Log.Debug("Setting up database with {ConnectionString}", connectionString);

Log.Logger = new LoggerConfiguration()
                            .WriteTo.RollingFile(logfilepath)
                            .WriteTo.SomeDatabaseSink(connectionString)
                            .CreateLogger();

Is there a cleaner, more straightforward approach to change the logger configuration other than overwriting it completely?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

13reactions
nblumhardtcommented, Nov 4, 2015

Thanks for the weigh-in @damianh. I agree there are some rough edges around this scenario, especially existing contextual loggers, as you point out. I think the way existing loggers continue to point to the sinks they were created with falls out from the immutable/stateless design, which isn’t ideal here but brings a lot of other benefits. Do you think your workaround is something that could be made general? It seems like the best approach available at this point… 😃

I think the new WriteTo.Logger() method added in the latest Serilog gets rid of your casting issue:

Log.Logger = new LoggerConfiguration()
      .WriteTo.Logger(Log.Logger)
      .WriteTo.Console().CreateLogger();
6reactions
nblumhardtcommented, Jul 9, 2015

Here’s one option that’s debatably better:

// <snip>
Log.Debug("Setting up database with {ConnectionString}", connectionString);

Log.Logger = new LoggerConfiguration()
                            .WriteTo.Sink((ILogEventSink)Log.Logger)
                            .WriteTo.SomeDatabaseSink(connectionString)
                            .CreateLogger();

Instead of letting the old logger drop, the new one just redirects to it.

It’s not ideal, but if you set up enrichment etc. on the second (rather than the first) logger it might cut a bit of duplication/noise out. It also has the property of being less likely to cause file locking issues.

👍 or 👎 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serilog : choose which sink to log at runtime
Context; using Serilog.Filters; // ... Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() .Enrich.FromLogContext() .WriteTo.
Read more >
Hot-reload any Serilog sink
Configuration's runtime reconfiguration logic only supports minimum levels, ... if it needs to change at runtime without reloading the app, ...
Read more >
Configuring a Platform - Nuclio
Want to learn about Configuring a Platform? ... First, you create a named logger sink and provide it with configuration. ... Runtime (...
Read more >
Flume 1.11.0 User Guide
The configuration includes properties of each source, sink and channel in an agent ... to a Logger Sink, which will output all event...
Read more >
Changing Serilog Minimum level without application restart ...
There are many ways to configure Serilog. The configuration library has the additional advantage that it supports dynamic reloading of the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found