fix(scripting): companion sink falls back to ScriptId for the main-log mirror (T3 review)

This commit is contained in:
Joseph Doherty
2026-06-10 12:08:29 -04:00
parent bd2dd05a0c
commit 788bb68d1d
2 changed files with 32 additions and 1 deletions
@@ -47,7 +47,8 @@ public sealed class ScriptLogCompanionSink : ILogEventSink
if (logEvent.Level < _minMirrorLevel) return;
var scriptName = "unknown";
if (logEvent.Properties.TryGetValue(ScriptLoggerFactory.ScriptNameProperty, out var prop)
if ((logEvent.Properties.TryGetValue(ScriptLoggerFactory.ScriptNameProperty, out var prop)
|| logEvent.Properties.TryGetValue(ScriptLoggerFactory.ScriptIdProperty, out prop))
&& prop is ScalarValue sv && sv.Value is string s)
{
scriptName = s;
@@ -136,6 +136,36 @@ public sealed class ScriptLogCompanionSinkTests
Should.NotThrow(() => companion.Emit(ev));
}
/// <summary>
/// Tests that an Error event carrying only ScriptId (no ScriptName) is mirrored
/// with the ScriptId value — not the "unknown" fallback.
/// Evaluators built by ScriptLoggerFactory bind ScriptId, not ScriptName.
/// </summary>
[Fact]
public void ScriptId_used_as_fallback_when_ScriptName_absent()
{
var mainSink = new CapturingSink();
var mainLogger = new LoggerConfiguration()
.MinimumLevel.Verbose().WriteTo.Sink(mainSink).CreateLogger();
var scriptLogger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.WriteTo.Sink(new ScriptLogCompanionSink(mainLogger))
.CreateLogger();
// Bind ScriptId only — no ScriptName — as the evaluator path does.
scriptLogger.ForContext(ScriptLoggerFactory.ScriptIdProperty, "script-abc-123")
.Error("evaluator error");
mainSink.Events.Count.ShouldBe(1);
var forwarded = mainSink.Events[0];
// ScriptName property in the forwarded event must be the ScriptId value, not "unknown".
forwarded.Properties.ShouldContainKey("ScriptName");
((ScalarValue)forwarded.Properties["ScriptName"]).Value
.ShouldBe("script-abc-123",
"companion sink should fall back to ScriptId when ScriptName is absent");
}
/// <summary>Tests that null main logger is rejected.</summary>
[Fact]
public void Null_main_logger_rejected()