| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | |
| | | 3 | | using Serilog; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.Serilog; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for configuring <see cref="NeedlrSerilogBootstrapper"/> instances. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class NeedlrSerilogBootstrapperExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Applies a custom <see cref="LoggerConfiguration"/> to the bootstrapper. |
| | | 14 | | /// If not called, a default console sink is used. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="bootstrapper">The bootstrapper to configure.</param> |
| | | 17 | | /// <param name="configure">A delegate that configures the <see cref="LoggerConfiguration"/>.</param> |
| | | 18 | | /// <returns>A new <see cref="NeedlrSerilogBootstrapper"/> with the configuration applied.</returns> |
| | | 19 | | /// <example> |
| | | 20 | | /// <code> |
| | | 21 | | /// await new NeedlrSerilogBootstrapper() |
| | | 22 | | /// .Configure(cfg => cfg |
| | | 23 | | /// .MinimumLevel.Debug() |
| | | 24 | | /// .WriteTo.Console()) |
| | | 25 | | /// .RunAsync(async (ctx, ct) => { /* ... */ }); |
| | | 26 | | /// </code> |
| | | 27 | | /// </example> |
| | | 28 | | public static NeedlrSerilogBootstrapper Configure( |
| | | 29 | | this NeedlrSerilogBootstrapper bootstrapper, |
| | | 30 | | Action<LoggerConfiguration> configure) |
| | | 31 | | { |
| | 14 | 32 | | ArgumentNullException.ThrowIfNull(bootstrapper); |
| | 13 | 33 | | ArgumentNullException.ThrowIfNull(configure); |
| | 12 | 34 | | return bootstrapper with |
| | 12 | 35 | | { |
| | 12 | 36 | | ConfigureAction = configure, |
| | 12 | 37 | | ConfigureWithConfigAction = null, |
| | 12 | 38 | | }; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Applies a custom <see cref="LoggerConfiguration"/> that can read from the |
| | | 43 | | /// bootstrap-phase <see cref="IConfiguration"/>. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <remarks> |
| | | 46 | | /// <para> |
| | | 47 | | /// The <paramref name="configure"/> delegate receives the bootstrap |
| | | 48 | | /// <see cref="IConfiguration"/> as its second parameter. This is the same configuration |
| | | 49 | | /// built by <see cref="ConfigureBootstrapConfiguration"/> and is <strong>not</strong> |
| | | 50 | | /// the application's DI-provided <see cref="IConfiguration"/>. |
| | | 51 | | /// </para> |
| | | 52 | | /// <para> |
| | | 53 | | /// A common pattern is to use <c>cfg.ReadFrom.Configuration(bootstrapConfiguration)</c> |
| | | 54 | | /// to load Serilog settings from a JSON file during bootstrap, while the real application |
| | | 55 | | /// logger is configured separately by the DI container. |
| | | 56 | | /// </para> |
| | | 57 | | /// <para> |
| | | 58 | | /// If called multiple times, only the last call takes effect. This overload replaces |
| | | 59 | | /// any prior <see cref="Configure(NeedlrSerilogBootstrapper, Action{LoggerConfiguration})"/> |
| | | 60 | | /// call and vice versa. |
| | | 61 | | /// </para> |
| | | 62 | | /// </remarks> |
| | | 63 | | /// <param name="bootstrapper">The bootstrapper to configure.</param> |
| | | 64 | | /// <param name="configure"> |
| | | 65 | | /// A delegate that configures <see cref="LoggerConfiguration"/> using the bootstrap |
| | | 66 | | /// <see cref="IConfiguration"/>. The second parameter is named |
| | | 67 | | /// <c>bootstrapConfiguration</c> to emphasize it is not the application's configuration. |
| | | 68 | | /// </param> |
| | | 69 | | /// <returns>A new <see cref="NeedlrSerilogBootstrapper"/> with the configuration applied.</returns> |
| | | 70 | | /// <example> |
| | | 71 | | /// <code> |
| | | 72 | | /// await new NeedlrSerilogBootstrapper() |
| | | 73 | | /// .ConfigureBootstrapConfiguration(builder => builder |
| | | 74 | | /// .AddJsonFile("appsettings.json", optional: true)) |
| | | 75 | | /// .Configure((cfg, bootstrapConfiguration) => cfg |
| | | 76 | | /// .ReadFrom.Configuration(bootstrapConfiguration) |
| | | 77 | | /// .WriteTo.Console()) |
| | | 78 | | /// .RunAsync(async (ctx, ct) => { /* ... */ }); |
| | | 79 | | /// </code> |
| | | 80 | | /// </example> |
| | | 81 | | public static NeedlrSerilogBootstrapper Configure( |
| | | 82 | | this NeedlrSerilogBootstrapper bootstrapper, |
| | | 83 | | Action<LoggerConfiguration, IConfiguration> configure) |
| | | 84 | | { |
| | 5 | 85 | | ArgumentNullException.ThrowIfNull(bootstrapper); |
| | 4 | 86 | | ArgumentNullException.ThrowIfNull(configure); |
| | 3 | 87 | | return bootstrapper with |
| | 3 | 88 | | { |
| | 3 | 89 | | ConfigureWithConfigAction = configure, |
| | 3 | 90 | | ConfigureAction = null, |
| | 3 | 91 | | }; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Configures the bootstrap-phase <see cref="IConfiguration"/> by adding sources to the |
| | | 96 | | /// <see cref="IConfigurationBuilder"/>. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <remarks> |
| | | 99 | | /// <para> |
| | | 100 | | /// The bootstrap configuration is <strong>not</strong> the same <see cref="IConfiguration"/> |
| | | 101 | | /// that the application's DI container will provide. It exists only for the duration of the |
| | | 102 | | /// bootstrap callback and is disposed after the bootstrapper completes. Syringe (and the .NET |
| | | 103 | | /// Generic Host / WebApplication builder) builds its own <see cref="IConfiguration"/> |
| | | 104 | | /// independently. |
| | | 105 | | /// </para> |
| | | 106 | | /// <para> |
| | | 107 | | /// By default the bootstrap configuration is <strong>empty</strong>. Call this method to add |
| | | 108 | | /// JSON files, environment variables, in-memory collections, or any other |
| | | 109 | | /// <see cref="IConfigurationSource"/> needed during the bootstrap phase. |
| | | 110 | | /// </para> |
| | | 111 | | /// <para> |
| | | 112 | | /// If called multiple times, only the last call takes effect. |
| | | 113 | | /// </para> |
| | | 114 | | /// </remarks> |
| | | 115 | | /// <param name="bootstrapper">The bootstrapper to configure.</param> |
| | | 116 | | /// <param name="configure"> |
| | | 117 | | /// A delegate that adds configuration sources to the <see cref="IConfigurationBuilder"/>. |
| | | 118 | | /// </param> |
| | | 119 | | /// <returns>A new <see cref="NeedlrSerilogBootstrapper"/> with the configuration builder registered.</returns> |
| | | 120 | | /// <example> |
| | | 121 | | /// <code> |
| | | 122 | | /// await new NeedlrSerilogBootstrapper() |
| | | 123 | | /// .ConfigureBootstrapConfiguration(builder => builder |
| | | 124 | | /// .AddJsonFile("appsettings.json", optional: true) |
| | | 125 | | /// .AddEnvironmentVariables()) |
| | | 126 | | /// .RunAsync(async (ctx, ct) => |
| | | 127 | | /// { |
| | | 128 | | /// var logDir = ctx.BootstrapConfiguration["Logging:Directory"]; |
| | | 129 | | /// }); |
| | | 130 | | /// </code> |
| | | 131 | | /// </example> |
| | | 132 | | public static NeedlrSerilogBootstrapper ConfigureBootstrapConfiguration( |
| | | 133 | | this NeedlrSerilogBootstrapper bootstrapper, |
| | | 134 | | Action<IConfigurationBuilder> configure) |
| | | 135 | | { |
| | 5 | 136 | | ArgumentNullException.ThrowIfNull(bootstrapper); |
| | 4 | 137 | | ArgumentNullException.ThrowIfNull(configure); |
| | 3 | 138 | | return bootstrapper with { ConfigureBootstrapConfigurationBuilder = configure }; |
| | | 139 | | } |
| | | 140 | | } |