Class BootstrapLogger<TCategoryName>
Provides a refactor-friendly pre-host logger for early startup logging.
Inherited Members
Namespace: Eigenverft.NetLib.Infrastructure.Hosting.Logging.BootstrapLogger
Assembly: Eigenverft.NetLib.Infrastructure.dll
Syntax
public static class BootstrapLogger<TCategoryName>
Type Parameters
| Name | Description |
|---|---|
| TCategoryName | The category type used to name the logger. |
Remarks
Intended for logging before the host is built, when DI resolution of ILoggerFactory is not available. The resulting logger is deliberately a stable, process-wide bootstrap channel. It is not rebound when the application later replaces Serilog's global logger or configures the host logging pipeline. This separation lets startup diagnostics inspect configuration and logging setup without depending on the setup being diagnosed.
Resolution strategy on the first call:
- If Serilog and the Serilog-to-MEL bridge are available and the current global logger is not Serilog's built-in default silent logger, that logger is captured by a Serilog-backed ILoggerFactory.
- Serilog's built-in default silent logger is treated as "not explicitly initialized" and uses the Microsoft fallback. Sink configuration is otherwise not inferred; every explicitly assigned Serilog logger is accepted as intentional.
- Otherwise, a minimal Microsoft logging factory is created with Console support when available, optionally applying the "Logging" configuration section when possible.
The first successful factory creation wins for the process. Later configuration arguments and later application logging changes intentionally do not alter existing bootstrap loggers.
Methods
| Edit this page View SourceCreateLogger(IConfiguration?)
Creates a pre-host ILogger<TCategoryName> using TCategoryName as category.
Declaration
public static ILogger<TCategoryName> CreateLogger(IConfiguration? configuration = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IConfiguration | configuration | Optional configuration used only when the process-wide factory is first created. It may apply the "Logging" section to the Microsoft fallback factory when the required logging configuration package is available. |
Returns
| Type | Description |
|---|---|
| ILogger<TCategoryName> | A logger instance that can be used prior to building the host. |
Examples
ILogger startupLogger = BootstrapLogger<Program>.CreateLogger(builder.Configuration);
|
Edit this page
View Source
CreateLoggerFactory(IConfiguration?)
Creates (or returns the cached) ILoggerFactory suitable for pre-host usage.
Declaration
public static ILoggerFactory CreateLoggerFactory(IConfiguration? configuration = null)
Parameters
| Type | Name | Description |
|---|---|---|
| IConfiguration | configuration | Optional configuration used to apply the "Logging" section (levels and filters) if available. |
Returns
| Type | Description |
|---|---|
| ILoggerFactory | The process-owned cached ILoggerFactory. Callers may create loggers from it but must not dispose it. |
CreateRequiredSerilogLogger(string, string?, string, bool)
Creates a required Serilog-backed pre-host logger from one explicit JSON configuration file.
Declaration
public static ILogger<TCategoryName> CreateRequiredSerilogLogger(string configurationFile = "AppSettings/BootstrapLoggerSettings.json", string? baseDirectory = null, string sectionName = "Serilog", bool reloadOnChange = false)
Parameters
| Type | Name | Description |
|---|---|---|
| string | configurationFile | The JSON file containing the isolated bootstrap Serilog configuration. Relative paths are resolved beneath
|
| string | baseDirectory | The base directory used to resolve a relative |
| string | sectionName | The configuration section consumed by Serilog.Settings.Configuration. The default is |
| bool | reloadOnChange | Enables the JSON configuration provider's change notifications. The default is false so the bootstrap channel remains completely stable. When enabled, Serilog can update existing minimum-level overrides and level switches; it does not rebuild or replace the configured sink pipeline. |
Returns
| Type | Description |
|---|---|
| ILogger<TCategoryName> | A stable ILogger<TCategoryName> backed by the Serilog instance created from the file. |
Remarks
This strict API must not be confused with CreateLogger(IConfiguration?). The automatic method uses an already initialized Serilog logger when one exists and otherwise falls back to Microsoft logging. This method instead explicitly creates Serilog from the named file and never falls back.
The bootstrap configuration is intentionally isolated. It does not load ASP.NET Core defaults, environment-specific companion files, environment variables, command-line arguments, DPAPI decoding, or the later application logging configuration. This separation allows the static bootstrap logger to report failures in those later configuration stages without depending on them.
Serilog core, Serilog.Settings.Configuration, Serilog.Extensions.Logging, and every sink or enricher named by the JSON file must be supplied by the consuming application. The NetLib has no compile-time Serilog reference and uses reflection exclusively. Missing components, invalid JSON, and incompatible Serilog APIs produce descriptive startup exceptions. This method must be the first BootstrapLogger initialization in the process; later replacement of the global Serilog logger does not rebind the created bootstrap channel.
When called from a static field initializer, failures may occur before the Main method body is entered and can
therefore surface as a type-initialization exception. This fail-fast behavior is intentional for a required provider.