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
@@ -148,22 +148,30 @@ public static class ZbMetricsEndpointExtensions
```csharp
namespace ZB.MOM.WW.Telemetry.Serilog;
/// Extension point for configuring the Serilog two-stage bootstrap on an IHostApplicationBuilder.
/// Extension point for registering the Serilog application logger in DI on an IHostApplicationBuilder.
public static class ZbSerilogExtensions
{
/// Two-stage Serilog bootstrap:
/// Stage 1 — minimal console-only bootstrap logger (for startup errors before IConfiguration).
/// Stage 2 — application logger wired from IConfiguration (ReadFrom.Configuration reads
/// Serilog:WriteTo sinks + Serilog:MinimumLevel from "Serilog:MinimumLevel") with
/// fixed enrichers: SiteId + NodeRole from ZbTelemetryOptions; NodeHostname from
/// Environment.MachineName (auto — not a caller-supplied option); TraceContextEnricher;
/// and RedactionEnricher (applied only when ILogRedactor is registered).
/// Registers the Serilog application logger in DI. Wires configuration-driven sinks
/// (ReadFrom.Configuration reads Serilog:WriteTo sinks + Serilog:MinimumLevel) with
/// fixed enrichers: SiteId + NodeRole from ZbTelemetryOptions; NodeHostname from
/// Environment.MachineName (auto — not a caller-supplied option); TraceContextEnricher;
/// and RedactionEnricher (applied only when ILogRedactor is registered).
///
/// MinimumLevel: AddZbSerilog reads "Serilog:MinimumLevel" from IConfiguration. Callers that
/// bind MinimumLevel from a different config key (e.g. ScadaBridge's
/// "ScadaBridge:Logging:MinimumLevel") apply that override themselves before or after
/// calling AddZbSerilog — this remains per-project and AddZbSerilog does not read it.
///
/// IMPORTANT — no process-global state: AddZbSerilog does NOT set Log.Logger. It passes
/// preserveStaticLogger: true to AddSerilog so the static logger is left untouched.
/// This makes AddZbSerilog safe to call multiple times in one process (integration tests,
/// multi-host apps) without hitting "The logger is already frozen".
///
/// Apps that need a pre-Build() bootstrap logger (for startup exceptions before IConfiguration
/// is available) should set Log.Logger themselves in Program.cs:
/// Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateBootstrapLogger();
/// That is an application-level decision — not done by this library.
///
/// OTel log export is wired automatically: logs flow through the OTel pipeline with the same
/// Resource as the metrics and traces (all three signals correlated in a backend).
///
@@ -247,10 +255,12 @@ The net48 x86 mxaccessgw worker is excluded from both packages. Its `IWorkerLogg
(`Directory.Packages.props`).
3. **`RedactionEnricher` DI timing:** `RedactionEnricher` resolves `ILogRedactor` from
`IServiceProvider` on first use (lazy, to avoid a circular-DI problem during Serilog's
two-stage bootstrap). Validate that the service provider is fully built by the time the
first post-startup log event fires. If MxGateway's `GatewayLogRedactor` has dependencies
that are not available at stage-1 bootstrap time, the lazy-resolve pattern protects it.
`IServiceProvider` on first use (lazy, to avoid a circular-DI problem at host-build time).
Validate that the service provider is fully built by the time the first post-startup log
event fires. If MxGateway's `GatewayLogRedactor` has dependencies that are not yet
available when the DI container is being composed, the lazy-resolve pattern protects it.
(Note: the library no longer sets a Stage-1 bootstrap logger, so there is no Stage-1 vs.
Stage-2 logger lifetime to reason about — only the single DI-registered application logger.)
4. **`SiteId` / `NodeRole` null handling:** `AddZbTelemetry` and `AddZbSerilog` silently
omit null `SiteId`/`NodeRole` from the Resource and enricher set. Confirm this is the