Skip to content

NeedlrSerilogBootstrapper

NexusLabs.Needlr.Serilog

NeedlrSerilogBootstrapper Class

Wraps an application entry point with Serilog-specific bootstrap lifecycle management: two-stage initialization, a pre-DI Serilog logger, top-level exception handling, and automatic log flushing on shutdown.

public sealed record NeedlrSerilogBootstrapper : System.IEquatable<NexusLabs.Needlr.Serilog.NeedlrSerilogBootstrapper>

Inheritance System.Object 🡒 NeedlrSerilogBootstrapper

Implements System.IEquatable<NeedlrSerilogBootstrapper>

Example

await new NeedlrSerilogBootstrapper()
    .Configure(cfg => cfg
        .MinimumLevel.Debug()
        .WriteTo.Console())
    .RunAsync(async (ctx, ct) =>
    {
        var webApp = new Syringe()
            .UsingSourceGen()
            .ForWebApplication()
            .UsingOptions(() => CreateWebApplicationOptions.Default
                .UsingCurrentProcessCliArgs()
                .UsingLogger(ctx.Logger))
            .BuildWebApplication();

        await webApp.RunAsync(ct);
    });

Remarks

NeedlrSerilogBootstrapper composes NexusLabs.Needlr.Hosting.NeedlrBootstrapper internally. All lifecycle behaviour (exception catching, cleanup, logger factory ownership) is delegated to NexusLabs.Needlr.Hosting.NeedlrBootstrapper — this type only adds Serilog-specific wiring: setting Log.Logger before the callback runs and flushing it in finally.

By default a console sink is configured. Override with Configure(this NeedlrSerilogBootstrapper, Action<LoggerConfiguration>) to apply your own Serilog.LoggerConfiguration.

Methods

NeedlrSerilogBootstrapper.RunAsync(Func<NeedlrBootstrapContext,CancellationToken,Task>, CancellationToken) Method

Runs the application entry point with Serilog bootstrap lifecycle management.

public System.Threading.Tasks.Task RunAsync(System.Func<NexusLabs.Needlr.Hosting.NeedlrBootstrapContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> runAsync, System.Threading.CancellationToken cancellationToken=default(System.Threading.CancellationToken));

Parameters

runAsync System.Func<NexusLabs.Needlr.Hosting.NeedlrBootstrapContext,System.Threading.CancellationToken,System.Threading.Tasks.Task>

The application callback. Receives a NexusLabs.Needlr.Hosting.NeedlrBootstrapContext containing a bootstrap logger backed by the configured Serilog pipeline.

cancellationToken System.Threading.CancellationToken

Optional cancellation token forwarded to the callback.

Returns

System.Threading.Tasks.Task
A System.Threading.Tasks.Task that completes when the application exits.