f569d537d1
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.
103 lines
3.8 KiB
C#
103 lines
3.8 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Serilog;
|
|
using ZB.MOM.WW.Telemetry;
|
|
using ZB.MOM.WW.Telemetry.Serilog;
|
|
|
|
namespace ZB.MOM.WW.Telemetry.Serilog.Tests;
|
|
|
|
/// <summary>
|
|
/// Regression tests for the process-global-state hazard: AddZbSerilog must not set or
|
|
/// freeze Log.Logger. When multiple hosts are built in the same process (integration
|
|
/// tests, multi-host apps) AddZbSerilog must be callable repeatedly without throwing
|
|
/// "The logger is already frozen".
|
|
/// </summary>
|
|
public sealed class MultiHostTests
|
|
{
|
|
[Fact]
|
|
public void AddZbSerilog_called_twice_in_same_process_does_not_throw()
|
|
{
|
|
// Arrange + Act: build two completely independent hosts in the same test process.
|
|
// Prior to the fix, the second call to AddZbSerilog would crash with
|
|
// "The logger is already frozen" because Stage-1 set the process-global Log.Logger.
|
|
var exception = Record.Exception(() =>
|
|
{
|
|
var builder1 = Host.CreateApplicationBuilder();
|
|
builder1.AddZbSerilog(o =>
|
|
{
|
|
o.ServiceName = "host-one";
|
|
o.SiteId = "s1";
|
|
o.NodeRole = "central";
|
|
});
|
|
using var host1 = builder1.Build();
|
|
|
|
var builder2 = Host.CreateApplicationBuilder();
|
|
builder2.AddZbSerilog(o =>
|
|
{
|
|
o.ServiceName = "host-two";
|
|
o.SiteId = "s2";
|
|
o.NodeRole = "site";
|
|
});
|
|
using var host2 = builder2.Build();
|
|
});
|
|
|
|
Assert.Null(exception);
|
|
}
|
|
|
|
[Fact]
|
|
public void AddZbSerilog_does_not_mutate_global_Log_Logger()
|
|
{
|
|
// Capture whatever the static logger is before calling AddZbSerilog.
|
|
var loggerBefore = Log.Logger;
|
|
|
|
var builder = Host.CreateApplicationBuilder();
|
|
builder.AddZbSerilog(o =>
|
|
{
|
|
o.ServiceName = "no-global-state";
|
|
});
|
|
using var host = builder.Build();
|
|
|
|
// AddZbSerilog must leave Log.Logger exactly as it was found.
|
|
// (ReferenceEquals is the right check — it must be the *same* instance, not
|
|
// just an equivalent one, so we know the library never touched the static field.)
|
|
Assert.True(
|
|
ReferenceEquals(loggerBefore, Log.Logger),
|
|
"AddZbSerilog must not replace or freeze the global Log.Logger");
|
|
}
|
|
|
|
[Fact]
|
|
public void AddZbSerilog_each_host_resolves_its_own_DI_ILogger()
|
|
{
|
|
// Both hosts must resolve a working Serilog ILogger from DI independently —
|
|
// neither host's logger is the process-global Log.Logger.
|
|
var builder1 = Host.CreateApplicationBuilder();
|
|
builder1.AddZbSerilog(o => { o.ServiceName = "host-a"; });
|
|
using var host1 = builder1.Build();
|
|
|
|
var builder2 = Host.CreateApplicationBuilder();
|
|
builder2.AddZbSerilog(o => { o.ServiceName = "host-b"; });
|
|
using var host2 = builder2.Build();
|
|
|
|
var logger1 = host1.Services.GetRequiredService<ILogger>();
|
|
var logger2 = host2.Services.GetRequiredService<ILogger>();
|
|
|
|
// Both are non-null and independently functional.
|
|
Assert.NotNull(logger1);
|
|
Assert.NotNull(logger2);
|
|
|
|
// They are distinct instances (each host has its own application logger).
|
|
Assert.False(
|
|
ReferenceEquals(logger1, logger2),
|
|
"each host must have its own DI-registered ILogger instance");
|
|
|
|
// Neither matches the global Log.Logger — the library must not have promoted
|
|
// a DI logger to process-global state.
|
|
Assert.False(
|
|
ReferenceEquals(logger1, Log.Logger),
|
|
"host-a's DI logger must not be the global Log.Logger");
|
|
Assert.False(
|
|
ReferenceEquals(logger2, Log.Logger),
|
|
"host-b's DI logger must not be the global Log.Logger");
|
|
}
|
|
}
|