A bootstrap logger #28
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We want to configure Serilog as soon as possible during program start-up, so that errors during start-up are logged. But, many logging-relevant services are not available until part-way through host bootstrapping. Instead of being stuck with an either/or choice, this PR attempts to address both requirements:
Log.Logger
Overall, there should be no regressions for existing code, since this mechanism is opt-in using the new bootstrap logger type (though I've taken the liberty of updating minimum framework/dependency versions, which is a breaking change).
Code using this path should suffer little to no performance penalty, since the
Logger
that ends up registered with the host is the underlying frozen/final logger.Example
There's a web application example included in the changeset. Its
Program.cs
looks like this:Initially, a console logger is configured. Note the use of
CreateBootstrapLogger()
, a new method in this PR.We write an event through it, and if bootstrapping fails, we'll also write the exception to it. The initial logger could be configured using any combination of sinks, etc.
Then, in
CreateHostBuilder()
, the logger is reconfigured: we again addConsole
, but also read fromappSettings.json
to add a file logger, and inject sinks etc. from theIServiceProvider
(also a new method, here).The output to the console is:
Note the initial "Starting up!".
The output to the log file is:
Note the final "Stopped cleanly" - the reconfigured logger outlives the host.