| | | 1 | | using NexusLabs.Needlr.Hosting; |
| | | 2 | | |
| | | 3 | | using Serilog; |
| | | 4 | | using Serilog.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | using Microsoft.Extensions.Configuration; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.Serilog; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Wraps an application entry point with Serilog-specific bootstrap lifecycle management: |
| | | 12 | | /// two-stage initialization, a pre-DI Serilog logger, pre-DI <see cref="IConfiguration"/>, |
| | | 13 | | /// top-level exception handling, and automatic log flushing on shutdown. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <para> |
| | | 17 | | /// <see cref="NeedlrSerilogBootstrapper"/> composes <see cref="NeedlrBootstrapper"/> internally. |
| | | 18 | | /// All lifecycle behaviour (exception catching, cleanup, logger factory ownership) is delegated |
| | | 19 | | /// to <see cref="NeedlrBootstrapper"/> — this type only adds Serilog-specific wiring: |
| | | 20 | | /// setting <c>Log.Logger</c> before the callback runs and flushing it in <c>finally</c>. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// By default a console sink is configured. Override with |
| | | 24 | | /// <see cref="NeedlrSerilogBootstrapperExtensions.Configure(NeedlrSerilogBootstrapper, Action{LoggerConfiguration})"/> |
| | | 25 | | /// to apply your own <see cref="LoggerConfiguration"/>, or use |
| | | 26 | | /// <see cref="NeedlrSerilogBootstrapperExtensions.Configure(NeedlrSerilogBootstrapper, Action{LoggerConfiguration, ICon |
| | | 27 | | /// to configure Serilog using the bootstrap <see cref="IConfiguration"/>. |
| | | 28 | | /// </para> |
| | | 29 | | /// <para> |
| | | 30 | | /// By default the bootstrap configuration is <strong>empty</strong>. Use |
| | | 31 | | /// <see cref="NeedlrSerilogBootstrapperExtensions.ConfigureBootstrapConfiguration"/> to add |
| | | 32 | | /// configuration sources needed during the bootstrap phase. |
| | | 33 | | /// The bootstrap configuration is <strong>not</strong> the same <see cref="IConfiguration"/> |
| | | 34 | | /// that the application's DI container will provide. |
| | | 35 | | /// </para> |
| | | 36 | | /// </remarks> |
| | | 37 | | /// <example> |
| | | 38 | | /// <code> |
| | | 39 | | /// await new NeedlrSerilogBootstrapper() |
| | | 40 | | /// .ConfigureBootstrapConfiguration(builder => builder |
| | | 41 | | /// .AddJsonFile("appsettings.json", optional: true)) |
| | | 42 | | /// .Configure((cfg, bootstrapConfiguration) => cfg |
| | | 43 | | /// .ReadFrom.Configuration(bootstrapConfiguration) |
| | | 44 | | /// .WriteTo.Console()) |
| | | 45 | | /// .RunAsync(async (ctx, ct) => |
| | | 46 | | /// { |
| | | 47 | | /// var webApp = new Syringe() |
| | | 48 | | /// .UsingSourceGen() |
| | | 49 | | /// .ForWebApplication() |
| | | 50 | | /// .UsingOptions(() => CreateWebApplicationOptions.Default |
| | | 51 | | /// .UsingCurrentProcessCliArgs() |
| | | 52 | | /// .UsingLogger(ctx.Logger)) |
| | | 53 | | /// .BuildWebApplication(); |
| | | 54 | | /// |
| | | 55 | | /// await webApp.RunAsync(ct); |
| | | 56 | | /// }); |
| | | 57 | | /// </code> |
| | | 58 | | /// </example> |
| | | 59 | | [DoNotAutoRegister] |
| | | 60 | | public sealed record NeedlrSerilogBootstrapper |
| | | 61 | | { |
| | 36 | 62 | | internal Action<LoggerConfiguration>? ConfigureAction { get; init; } |
| | 30 | 63 | | internal Action<LoggerConfiguration, IConfiguration>? ConfigureWithConfigAction { get; init; } |
| | 31 | 64 | | internal Action<IConfigurationBuilder>? ConfigureBootstrapConfigurationBuilder { get; init; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Runs the application entry point with Serilog bootstrap lifecycle management. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="runAsync"> |
| | | 70 | | /// The application callback. Receives a <see cref="NeedlrBootstrapContext"/> containing |
| | | 71 | | /// a bootstrap logger backed by the configured Serilog pipeline and a bootstrap |
| | | 72 | | /// <see cref="IConfiguration"/>. |
| | | 73 | | /// </param> |
| | | 74 | | /// <param name="cancellationToken"> |
| | | 75 | | /// Optional cancellation token forwarded to the callback. |
| | | 76 | | /// </param> |
| | | 77 | | /// <returns>A <see cref="Task"/> that completes when the application exits.</returns> |
| | | 78 | | public async Task RunAsync( |
| | | 79 | | Func<NeedlrBootstrapContext, CancellationToken, Task> runAsync, |
| | | 80 | | CancellationToken cancellationToken = default) |
| | | 81 | | { |
| | 14 | 82 | | ArgumentNullException.ThrowIfNull(runAsync); |
| | | 83 | | |
| | | 84 | | // Build bootstrap config early so the Serilog Configure delegate can use it. |
| | | 85 | | // This is built outside NeedlrBootstrapper so we can pass it to Serilog's |
| | | 86 | | // ReadFrom.Configuration before the inner bootstrapper runs. |
| | 13 | 87 | | var configBuilder = new ConfigurationBuilder(); |
| | 13 | 88 | | ConfigureBootstrapConfigurationBuilder?.Invoke(configBuilder); |
| | 13 | 89 | | IConfigurationRoot? bootstrapConfiguration = null; |
| | | 90 | | |
| | | 91 | | try |
| | | 92 | | { |
| | 13 | 93 | | bootstrapConfiguration = configBuilder.Build(); |
| | | 94 | | |
| | 13 | 95 | | var configuration = new LoggerConfiguration(); |
| | 13 | 96 | | if (ConfigureWithConfigAction is not null) |
| | | 97 | | { |
| | 2 | 98 | | ConfigureWithConfigAction(configuration, bootstrapConfiguration); |
| | | 99 | | } |
| | 11 | 100 | | else if (ConfigureAction is not null) |
| | | 101 | | { |
| | 10 | 102 | | ConfigureAction(configuration); |
| | | 103 | | } |
| | | 104 | | else |
| | | 105 | | { |
| | 1 | 106 | | configuration.WriteTo.Console(); |
| | | 107 | | } |
| | | 108 | | |
| | 12 | 109 | | Log.Logger = configuration.CreateLogger(); |
| | 12 | 110 | | } |
| | | 111 | | catch (Exception ex) |
| | | 112 | | { |
| | | 113 | | // Serilog configuration failed — fall back to a bare console logger so |
| | | 114 | | // the error is visible, then rethrow into the inner bootstrapper's |
| | | 115 | | // catch/cleanup path via a wrapper callback. |
| | 1 | 116 | | Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger(); |
| | 1 | 117 | | Log.Fatal(ex, "Failed to configure Serilog bootstrap logger."); |
| | | 118 | | |
| | | 119 | | // Let the inner bootstrapper handle cleanup. The callback will throw |
| | | 120 | | // the original exception so it is logged at Critical by NeedlrBootstrapper. |
| | 1 | 121 | | var capturedEx = ex; |
| | 1 | 122 | | await RunInnerBootstrapper( |
| | 1 | 123 | | bootstrapConfiguration, |
| | 1 | 124 | | (_, _) => throw capturedEx, |
| | 1 | 125 | | cancellationToken) |
| | 1 | 126 | | .ConfigureAwait(false); |
| | 1 | 127 | | return; |
| | | 128 | | } |
| | | 129 | | |
| | 12 | 130 | | await RunInnerBootstrapper( |
| | 12 | 131 | | bootstrapConfiguration, |
| | 12 | 132 | | runAsync, |
| | 12 | 133 | | cancellationToken) |
| | 12 | 134 | | .ConfigureAwait(false); |
| | 13 | 135 | | } |
| | | 136 | | |
| | | 137 | | private async Task RunInnerBootstrapper( |
| | | 138 | | IConfigurationRoot? bootstrapConfiguration, |
| | | 139 | | Func<NeedlrBootstrapContext, CancellationToken, Task> runAsync, |
| | | 140 | | CancellationToken cancellationToken) |
| | | 141 | | { |
| | 13 | 142 | | var loggerFactory = new SerilogLoggerFactory(dispose: false); |
| | | 143 | | |
| | 13 | 144 | | var inner = new NeedlrBootstrapper() |
| | 13 | 145 | | .UsingLoggerFactory(loggerFactory) |
| | 26 | 146 | | .WithCleanup(() => Log.CloseAndFlushAsync().AsTask()); |
| | | 147 | | |
| | 13 | 148 | | if (ConfigureBootstrapConfigurationBuilder is not null) |
| | | 149 | | { |
| | 2 | 150 | | inner = inner.ConfigureBootstrapConfiguration(ConfigureBootstrapConfigurationBuilder); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | // If we already built bootstrap config for Serilog, override the inner |
| | | 154 | | // bootstrapper's config building to reuse the same instance instead of |
| | | 155 | | // building it twice. We pass a no-op configure action — the inner |
| | | 156 | | // bootstrapper will still build a ConfigurationBuilder, but we need |
| | | 157 | | // it to have the same sources. Simpler: we forward the same configure action. |
| | | 158 | | // The inner bootstrapper will build its own IConfigurationRoot from the |
| | | 159 | | // same sources — this is acceptable because bootstrap config is cheap |
| | | 160 | | // and the Serilog config phase is done. |
| | | 161 | | |
| | 13 | 162 | | await inner.RunAsync(runAsync, cancellationToken) |
| | 13 | 163 | | .ConfigureAwait(false); |
| | | 164 | | |
| | | 165 | | // Dispose our pre-built config after the inner bootstrapper has |
| | | 166 | | // disposed its own copy and completed cleanup. |
| | 13 | 167 | | (bootstrapConfiguration as IDisposable)?.Dispose(); |
| | 13 | 168 | | } |
| | | 169 | | } |