refactor(telemetry.serilog): review fixes (thread-safe redactor, bootstrap logger, minlevel ordering, test coverage)

This commit is contained in:
Joseph Doherty
2026-06-01 07:48:57 -04:00
parent 37fb84f477
commit f1240c0bd4
7 changed files with 140 additions and 48 deletions
@@ -1,5 +1,3 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
@@ -8,22 +6,46 @@ using ZB.MOM.WW.Telemetry;
namespace ZB.MOM.WW.Telemetry.Serilog;
/// <summary>
/// Extension point for configuring the shared Serilog bootstrap on an
/// Extension point for configuring the shared Serilog two-stage bootstrap on an
/// <see cref="IHostApplicationBuilder"/>. Wires config-driven sinks
/// (<c>ReadFrom.Configuration</c>), an explicit minimum level (<c>Serilog:MinimumLevel</c>,
/// default <see cref="LogEventLevel.Information"/>), and the shared enricher/redaction/OTel-export
/// set via <see cref="ZbSerilogConfig"/>. Does NOT configure OTel metrics/traces — call
/// <c>AddZbTelemetry</c> in the core package for that.
///
/// <para>
/// Stage 1 (bootstrap): sets <see cref="Log.Logger"/> to a console-only logger before
/// <c>Build()</c> is called, capturing any startup exceptions that occur before the DI
/// container is ready.
/// </para>
/// <para>
/// Stage 2 (application): wires the full Serilog pipeline via
/// <see cref="SerilogHostBuilderExtensions.UseSerilog(IHostBuilder,Action{HostBuilderContext,IServiceProvider,LoggerConfiguration},bool,bool)"/>
/// once the host builds, replacing the bootstrap logger with the configuration-driven one.
/// </para>
/// </summary>
public static class ZbSerilogExtensions
{
/// <summary>
/// Registers Serilog on the host with the shared ZB.MOM.WW configuration: sinks read from
/// <see cref="IConfiguration"/> (<c>ReadFrom.Configuration</c>), an explicit minimum level
/// (<c>Serilog:MinimumLevel</c>, default <see cref="LogEventLevel.Information"/>), and the
/// identity enrichers (<c>SiteId</c>/<c>NodeRole</c> from <paramref name="configure"/>,
/// <c>NodeHostname</c> = <see cref="System.Environment.MachineName"/>). The
/// <paramref name="configure"/> delegate receives the same <see cref="ZbTelemetryOptions"/>
/// Registers Serilog on the host with a two-stage bootstrap:
/// <list type="number">
/// <item>
/// <description>
/// Stage 1 — immediately sets <see cref="Log.Logger"/> to a console-only bootstrap logger
/// so that startup exceptions before <c>Build()</c> are captured.
/// </description>
/// </item>
/// <item>
/// <description>
/// Stage 2 — registers the full pipeline via <c>AddSerilog</c>: configuration-driven sinks
/// (<c>ReadFrom.Configuration</c>), a code default of <see cref="LogEventLevel.Information"/>
/// that config can override via <c>Serilog:MinimumLevel</c> or namespace overrides, plus
/// the identity enrichers (<c>SiteId</c>/<c>NodeRole</c> from <paramref name="configure"/>,
/// <c>NodeHostname</c> = <see cref="System.Environment.MachineName"/>).
/// </description>
/// </item>
/// </list>
/// The <paramref name="configure"/> delegate receives the same <see cref="ZbTelemetryOptions"/>
/// used by <c>AddZbTelemetry</c> — typically share one options-population lambda across both.
/// </summary>
/// <param name="builder">The host application builder.</param>
@@ -38,32 +60,21 @@ public static class ZbSerilogExtensions
var options = new ZbTelemetryOptions();
configure(options);
var minimumLevel = ReadMinimumLevel(builder.Configuration);
// Stage 1: bootstrap logger captures startup exceptions before Build().
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
// Stage 2: full application logger — code default first, then config overrides.
builder.Services.AddSerilog((serviceProvider, loggerConfiguration) =>
{
loggerConfiguration
.ReadFrom.Configuration(builder.Configuration)
.MinimumLevel.Is(minimumLevel);
.MinimumLevel.Is(LogEventLevel.Information)
.ReadFrom.Configuration(builder.Configuration);
ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider);
});
return builder;
}
/// <summary>
/// Reads <c>Serilog:MinimumLevel</c> (or the nested <c>Serilog:MinimumLevel:Default</c>) from
/// configuration, falling back to <see cref="LogEventLevel.Information"/> for missing or
/// unparseable values.
/// </summary>
private static LogEventLevel ReadMinimumLevel(IConfiguration configuration)
{
var raw = configuration["Serilog:MinimumLevel"]
?? configuration["Serilog:MinimumLevel:Default"];
return Enum.TryParse<LogEventLevel>(raw, ignoreCase: true, out var parsed)
? parsed
: LogEventLevel.Information;
}
}