fix(telemetry.serilog): don't set process-global Log.Logger in AddZbSerilog (multi-host safe)

Remove the Stage-1 bootstrap-logger line (Log.Logger = new LoggerConfiguration()
.WriteTo.Console().CreateBootstrapLogger()) from AddZbSerilog. A shared library must
not mutate process-global state: when multiple hosts are built in one process (integration
tests, Aspire multi-host, parallel test runs) the second call throws "The logger is
already frozen".

AddSerilog is now called with preserveStaticLogger: true so Serilog.Extensions.Hosting
leaves the static Log.Logger entirely untouched. The DI-registered application logger is
the only artifact AddZbSerilog produces.

Apps that want a pre-Build() bootstrap logger should set Log.Logger themselves in
Program.cs before calling AddZbSerilog — that decision belongs to the application.

Three new regression tests in MultiHostTests verify: two hosts build in the same process
without throwing; Log.Logger is not mutated; each host gets its own independent DI ILogger.

Docs (SPEC.md §5 and shared-contract ZB.MOM.WW.Telemetry.md) updated: the "two-stage
bootstrap" framing is replaced with the correct description — library registers only the
DI application logger; optional Stage-1 bootstrap is the app's responsibility.
This commit is contained in:
Joseph Doherty
2026-06-01 08:13:35 -04:00
parent f1240c0bd4
commit f569d537d1
4 changed files with 186 additions and 59 deletions
@@ -6,7 +6,7 @@ using ZB.MOM.WW.Telemetry;
namespace ZB.MOM.WW.Telemetry.Serilog;
/// <summary>
/// Extension point for configuring the shared Serilog two-stage bootstrap on an
/// Extension point for configuring the shared Serilog application logger 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
@@ -14,39 +14,44 @@ namespace ZB.MOM.WW.Telemetry.Serilog;
/// <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.
/// This method intentionally does <strong>not</strong> set the process-global
/// <see cref="Log.Logger"/> (via <c>CreateBootstrapLogger</c> or otherwise). Mutating
/// process-global state in a shared library causes "logger is already frozen" exceptions
/// when multiple hosts are built in the same process (integration tests, multi-host apps).
/// </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.
/// Apps that need a pre-<c>Build()</c> bootstrap logger to capture early startup exceptions
/// should set <see cref="Log.Logger"/> themselves in <c>Program.cs</c> before calling
/// <c>AddZbSerilog</c>:
/// <code>
/// Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateBootstrapLogger();
/// // ... then build the host ...
/// builder.AddZbSerilog(o => { ... });
/// </code>
/// This keeps global-state mutation firmly in the application, not the library.
/// </para>
/// </summary>
public static class ZbSerilogExtensions
{
/// <summary>
/// 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.
/// Registers the Serilog application logger in DI. Wires 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"/>).
///
/// <para>
/// This method does <strong>not</strong> set the process-global <see cref="Log.Logger"/>.
/// <c>preserveStaticLogger: true</c> is passed to <c>AddSerilog</c> so the static logger
/// is left entirely untouched — safe to call multiple times in the same process (integration
/// tests, multi-host scenarios) without hitting "logger is already frozen".
/// </para>
/// <para>
/// If early-startup bootstrap logging is required (before <c>Build()</c>), set
/// <c>Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateBootstrapLogger();</c>
/// in <c>Program.cs</c> before calling this method. That decision belongs to the
/// application, not the shared library.
/// </para>
/// </summary>
/// <param name="builder">The host application builder.</param>
/// <param name="configure">Populates the <see cref="ZbTelemetryOptions"/>.</param>
@@ -60,20 +65,19 @@ public static class ZbSerilogExtensions
var options = new ZbTelemetryOptions();
configure(options);
// Stage 1: bootstrap logger captures startup exceptions before Build().
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateBootstrapLogger();
// Register the application logger in DI only. preserveStaticLogger: true ensures
// AddSerilog does NOT freeze or replace Log.Logger — critical for multi-host
// processes (integration tests etc.) where AddZbSerilog may be called more than once.
builder.Services.AddSerilog(
(serviceProvider, loggerConfiguration) =>
{
loggerConfiguration
.MinimumLevel.Is(LogEventLevel.Information)
.ReadFrom.Configuration(builder.Configuration);
// Stage 2: full application logger — code default first, then config overrides.
builder.Services.AddSerilog((serviceProvider, loggerConfiguration) =>
{
loggerConfiguration
.MinimumLevel.Is(LogEventLevel.Information)
.ReadFrom.Configuration(builder.Configuration);
ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider);
});
ZbSerilogConfig.Apply(loggerConfiguration, options, serviceProvider);
},
preserveStaticLogger: true);
return builder;
}