| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | |
| | | 4 | | using Serilog; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.Serilog; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Auto-discovered plugin that wires Serilog as the logging provider using |
| | | 10 | | /// configuration from <c>appsettings.json</c> (or any <see cref="Microsoft.Extensions.Configuration.IConfiguration"/> |
| | | 11 | | /// source registered with Needlr). |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <para> |
| | | 15 | | /// This plugin provides a zero-ceremony default: reference the |
| | | 16 | | /// <c>NexusLabs.Needlr.Serilog</c> package and add a <c>"Serilog"</c> section |
| | | 17 | | /// to <c>appsettings.json</c>. The plugin is auto-discovered and configures |
| | | 18 | | /// <see cref="ILogger{TCategoryName}"/> resolution via DI. |
| | | 19 | | /// </para> |
| | | 20 | | /// <para> |
| | | 21 | | /// The Serilog logger is owned by the DI container — sinks flush automatically |
| | | 22 | | /// on container disposal. The static <see cref="Log.Logger"/> is not set — use |
| | | 23 | | /// <see cref="ILogger{TCategoryName}"/> injection instead, or use |
| | | 24 | | /// <see cref="NeedlrSerilogBootstrapper"/> for apps that need the static logger |
| | | 25 | | /// and two-stage bootstrap lifecycle. |
| | | 26 | | /// </para> |
| | | 27 | | /// </remarks> |
| | | 28 | | public sealed class SerilogPlugin : IServiceCollectionPlugin |
| | | 29 | | { |
| | | 30 | | public void Configure(ServiceCollectionPluginOptions options) |
| | | 31 | | { |
| | 13 | 32 | | var logger = new LoggerConfiguration() |
| | 13 | 33 | | .ReadFrom.Configuration(options.Config) |
| | 13 | 34 | | .CreateLogger(); |
| | | 35 | | |
| | 13 | 36 | | options.Services.AddLogging(builder => |
| | 13 | 37 | | { |
| | 13 | 38 | | builder.ClearProviders(); |
| | 13 | 39 | | builder.AddSerilog(logger, dispose: true); |
| | 26 | 40 | | }); |
| | 13 | 41 | | } |
| | | 42 | | } |