NeedlrSerilogBootstrapperExtensions
NexusLabs.Needlr.Serilog¶
NexusLabs.Needlr.Serilog¶
NeedlrSerilogBootstrapperExtensions Class¶
Extension methods for configuring NeedlrSerilogBootstrapper instances.
Inheritance System.Object 🡒 NeedlrSerilogBootstrapperExtensions
Methods¶
NeedlrSerilogBootstrapperExtensions.Configure(this NeedlrSerilogBootstrapper, Action<LoggerConfiguration,IConfiguration>) Method¶
Applies a custom Serilog.LoggerConfiguration that can read from the bootstrap-phase Microsoft.Extensions.Configuration.IConfiguration.
public static NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper Configure(this NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper bootstrapper, System.Action<Serilog.LoggerConfiguration,Microsoft.Extensions.Configuration.IConfiguration> configure);
Parameters¶
bootstrapper NeedlrSerilogBootstrapper
The bootstrapper to configure.
configure System.Action<Serilog.LoggerConfiguration,Microsoft.Extensions.Configuration.IConfiguration>
A delegate that configures Serilog.LoggerConfiguration using the bootstrap
Microsoft.Extensions.Configuration.IConfiguration. The second parameter is named
bootstrapConfiguration to emphasize it is not the application's configuration.
Returns¶
NeedlrSerilogBootstrapper
A new NeedlrSerilogBootstrapper with the configuration applied.
Example¶
await new NeedlrSerilogBootstrapper()
.ConfigureBootstrapConfiguration(builder => builder
.AddJsonFile("appsettings.json", optional: true))
.Configure((cfg, bootstrapConfiguration) => cfg
.ReadFrom.Configuration(bootstrapConfiguration)
.WriteTo.Console())
.RunAsync(async (ctx, ct) => { /* ... */ });
Remarks¶
The configure delegate receives the bootstrap Microsoft.Extensions.Configuration.IConfiguration as its second parameter. This is the same configuration built by ConfigureBootstrapConfiguration(this NeedlrSerilogBootstrapper, Action<IConfigurationBuilder>) and is not the application's DI-provided Microsoft.Extensions.Configuration.IConfiguration.
A common pattern is to use cfg.ReadFrom.Configuration(bootstrapConfiguration)
to load Serilog settings from a JSON file during bootstrap, while the real application
logger is configured separately by the DI container.
If called multiple times, only the last call takes effect. This overload replaces any prior Configure(this NeedlrSerilogBootstrapper, Action<LoggerConfiguration>) call and vice versa.
NeedlrSerilogBootstrapperExtensions.Configure(this NeedlrSerilogBootstrapper, Action<LoggerConfiguration>) Method¶
Applies a custom Serilog.LoggerConfiguration to the bootstrapper. If not called, a default console sink is used.
public static NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper Configure(this NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper bootstrapper, System.Action<Serilog.LoggerConfiguration> configure);
Parameters¶
bootstrapper NeedlrSerilogBootstrapper
The bootstrapper to configure.
configure System.Action<Serilog.LoggerConfiguration>
A delegate that configures the Serilog.LoggerConfiguration.
Returns¶
NeedlrSerilogBootstrapper
A new NeedlrSerilogBootstrapper with the configuration applied.
Example¶
await new NeedlrSerilogBootstrapper()
.Configure(cfg => cfg
.MinimumLevel.Debug()
.WriteTo.Console())
.RunAsync(async (ctx, ct) => { /* ... */ });
NeedlrSerilogBootstrapperExtensions.ConfigureBootstrapConfiguration(this NeedlrSerilogBootstrapper, Action<IConfigurationBuilder>) Method¶
Configures the bootstrap-phase Microsoft.Extensions.Configuration.IConfiguration by adding sources to the Microsoft.Extensions.Configuration.IConfigurationBuilder.
public static NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper ConfigureBootstrapConfiguration(this NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper bootstrapper, System.Action<Microsoft.Extensions.Configuration.IConfigurationBuilder> configure);
Parameters¶
bootstrapper NeedlrSerilogBootstrapper
The bootstrapper to configure.
configure System.Action<Microsoft.Extensions.Configuration.IConfigurationBuilder>
A delegate that adds configuration sources to the Microsoft.Extensions.Configuration.IConfigurationBuilder.
Returns¶
NeedlrSerilogBootstrapper
A new NeedlrSerilogBootstrapper with the configuration builder registered.
Example¶
await new NeedlrSerilogBootstrapper()
.ConfigureBootstrapConfiguration(builder => builder
.AddJsonFile("appsettings.json", optional: true)
.AddEnvironmentVariables())
.RunAsync(async (ctx, ct) =>
{
var logDir = ctx.BootstrapConfiguration["Logging:Directory"];
});
Remarks¶
The bootstrap configuration is not the same Microsoft.Extensions.Configuration.IConfiguration that the application's DI container will provide. It exists only for the duration of the bootstrap callback and is disposed after the bootstrapper completes. Syringe (and the .NET Generic Host / WebApplication builder) builds its own Microsoft.Extensions.Configuration.IConfiguration independently.
By default the bootstrap configuration is empty. Call this method to add JSON files, environment variables, in-memory collections, or any other Microsoft.Extensions.Configuration.IConfigurationSource needed during the bootstrap phase.
If called multiple times, only the last call takes effect.