ee8a8e9d3d
Task 5 code-review follow-ups: - I1: ExternalSystemHelper gates its TraceStepScope on a new isTraced ctor arg (default false for direct construction), not on `_traceSink is not null` — a traced run without an inline collector still writes durable ScriptLog rows that must name their step. - I2 (design §5): an inline shared script now SHARES its caller's ScriptLogger via the new `sharedLog` ctor param — one line cap, one anchor, one closing row. Consequence documented on the Log property: a shared script's lines carry the PARENT run's ExecutionId and land on the parent's ScriptRun node. - M3: TraceCapturingLogger swallows a throwing formatter — most of what it captures is best-effort "audit write failed" warnings logged from inside a catch block, and a formatting exception must not escape that path. - Tests: ApiCall step attribution, traced-without-collector, shared-script logger sharing, nested batch/wait scope attribution + unwind, throwing formatter; test (c) now also pins the anti-recursion property (EmitLog must log through the undecorated logger). CompileSurfaceParityTests stays red until Task 7 mirrors Log on ScriptCompileSurface.
190 lines
6.7 KiB
C#
190 lines
6.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using ZB.MOM.WW.ScadaBridge.SiteRuntime.Scripts;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteRuntime.Tests.Scripts;
|
|
|
|
/// <summary>
|
|
/// Flow Debugger step logs (design 2026-09-09 D6): the decorator installed over the script
|
|
/// context's logger while a run is traced. It must forward everything the node would have logged
|
|
/// anyway, capture Information and above as <c>runtime</c> rows — including levels the node's own
|
|
/// minimum level suppresses, because they are trace data, not node log output — and never capture
|
|
/// Debug/Trace chatter.
|
|
/// </summary>
|
|
public class TraceCapturingLoggerTests
|
|
{
|
|
/// <summary>One row the sink's emitter received.</summary>
|
|
private sealed record Row(string Level, string Source, string Message, Exception? Exception, Guid? StepEventId);
|
|
|
|
/// <summary>An inner logger that records what it was asked to write, at a configurable minimum level.</summary>
|
|
private sealed class RecordingLogger(LogLevel minimum = LogLevel.Trace) : ILogger
|
|
{
|
|
public List<(LogLevel Level, string Message)> Entries { get; } = [];
|
|
public List<object?> Scopes { get; } = [];
|
|
|
|
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
|
|
{
|
|
Scopes.Add(state);
|
|
return null;
|
|
}
|
|
|
|
public bool IsEnabled(LogLevel logLevel) => logLevel >= minimum;
|
|
|
|
public void Log<TState>(LogLevel level, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) =>
|
|
Entries.Add((level, formatter(state, exception)));
|
|
}
|
|
|
|
private static (TraceCapturingLogger Logger, RecordingLogger Inner, List<Row> Rows) Make(LogLevel innerMinimum = LogLevel.Trace)
|
|
{
|
|
var inner = new RecordingLogger(innerMinimum);
|
|
var rows = new List<Row>();
|
|
var sink = new ScriptLogger(
|
|
new RecordingLogger(),
|
|
(level, source, message, exception, stepEventId) => rows.Add(new Row(level, source, message, exception, stepEventId)),
|
|
maxLines: 200,
|
|
instance: "CvdReactor01",
|
|
script: "OnHeartbeat",
|
|
executionId: Guid.NewGuid());
|
|
return (new TraceCapturingLogger(inner, sink), inner, rows);
|
|
}
|
|
|
|
[Fact]
|
|
public void Debug_IsForwarded_ButNotCaptured()
|
|
{
|
|
var (logger, inner, rows) = Make();
|
|
|
|
logger.LogDebug("internal chatter");
|
|
|
|
Assert.Single(inner.Entries);
|
|
Assert.Empty(rows);
|
|
}
|
|
|
|
[Fact]
|
|
public void Warning_IsCapturedAsARuntimeRow_WithItsException()
|
|
{
|
|
var (logger, inner, rows) = Make();
|
|
var boom = new TimeoutException("no reply");
|
|
|
|
logger.LogWarning(boom, "tag write timed out after {Ms} ms", 5_000);
|
|
|
|
Assert.Single(inner.Entries);
|
|
var row = Assert.Single(rows);
|
|
Assert.Equal("warn", row.Level);
|
|
Assert.Equal("runtime", row.Source);
|
|
Assert.Equal("tag write timed out after 5000 ms", row.Message);
|
|
Assert.Same(boom, row.Exception);
|
|
}
|
|
|
|
[Fact]
|
|
public void ErrorAndCritical_AreBothCapturedAsError()
|
|
{
|
|
var (logger, _, rows) = Make();
|
|
|
|
logger.LogError("bad");
|
|
logger.LogCritical("worse");
|
|
|
|
Assert.Equal(["error", "error"], rows.Select(r => r.Level));
|
|
}
|
|
|
|
[Fact]
|
|
public void InformationIsEnabled_EvenWhenTheInnerLoggerSaysNo()
|
|
{
|
|
// A node whose Serilog minimum is Warning must still contribute Information lines to a trace.
|
|
var (logger, inner, rows) = Make(innerMinimum: LogLevel.Warning);
|
|
|
|
Assert.False(inner.IsEnabled(LogLevel.Information));
|
|
Assert.True(logger.IsEnabled(LogLevel.Information));
|
|
|
|
logger.LogInformation("captured anyway");
|
|
|
|
Assert.Empty(inner.Entries);
|
|
var row = Assert.Single(rows);
|
|
Assert.Equal("info", row.Level);
|
|
Assert.Equal("captured anyway", row.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void BelowInformation_TheInnerLoggerDecidesEnablement()
|
|
{
|
|
var (logger, _, _) = Make(innerMinimum: LogLevel.Warning);
|
|
|
|
Assert.False(logger.IsEnabled(LogLevel.Debug));
|
|
}
|
|
|
|
[Fact]
|
|
public void ADisabledInnerLogger_IsNotWrittenTo_ButTheRowIsStillCaptured()
|
|
{
|
|
var (logger, inner, rows) = Make(innerMinimum: LogLevel.Critical);
|
|
|
|
logger.LogWarning("suppressed on the node");
|
|
|
|
Assert.Empty(inner.Entries);
|
|
Assert.Single(rows);
|
|
}
|
|
|
|
[Fact]
|
|
public void CapturedLines_CarryTheAmbientStepId()
|
|
{
|
|
var (logger, _, rows) = Make();
|
|
var stepId = Guid.NewGuid();
|
|
|
|
using (TraceStepScope.Enter(stepId))
|
|
{
|
|
logger.LogInformation("inside the helper");
|
|
}
|
|
|
|
Assert.Equal(stepId, Assert.Single(rows).StepEventId);
|
|
}
|
|
|
|
[Fact]
|
|
public void Ctor_RejectsAnAlreadyDecoratedLogger()
|
|
{
|
|
// Wrapping twice would forward each script line through two capture stages and emit two
|
|
// runtime rows per line — and would burn the run's line cap at double rate.
|
|
var (already, _, _) = Make();
|
|
var sink = new ScriptLogger(new RecordingLogger(), emit: null, maxLines: 200, "i", "s", null);
|
|
|
|
var ex = Assert.Throws<ArgumentException>(() => new TraceCapturingLogger(already, sink));
|
|
Assert.Equal("inner", ex.ParamName);
|
|
}
|
|
|
|
[Fact]
|
|
public void Ctor_RequiresBothCollaborators()
|
|
{
|
|
var sink = new ScriptLogger(new RecordingLogger(), emit: null, maxLines: 200, "i", "s", null);
|
|
|
|
Assert.Throws<ArgumentNullException>(() => new TraceCapturingLogger(null!, sink));
|
|
Assert.Throws<ArgumentNullException>(() => new TraceCapturingLogger(new RecordingLogger(), null!));
|
|
}
|
|
|
|
[Fact]
|
|
public void AThrowingFormatter_IsSwallowed_AndDropsOnlyThatLine()
|
|
{
|
|
// Review M3. Most of what this decorator captures is the runtime's own best-effort
|
|
// "audit write failed" warnings, logged from INSIDE a catch block: a formatter exception
|
|
// escaping here would replace the original failure with a formatting one on a path whose
|
|
// contract is that it never throws. The inner logger is disabled for this level so the
|
|
// decorator's OWN formatter call is the only one under test.
|
|
var (logger, inner, rows) = Make(innerMinimum: LogLevel.Critical);
|
|
|
|
logger.Log(LogLevel.Warning, new EventId(0), "state", null,
|
|
(_, _) => throw new InvalidOperationException("formatter broke"));
|
|
|
|
Assert.Empty(rows);
|
|
Assert.Empty(inner.Entries);
|
|
|
|
// …and the decorator keeps working for the next line.
|
|
logger.LogWarning("still alive");
|
|
Assert.Equal("still alive", Assert.Single(rows).Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void BeginScope_DelegatesToTheInnerLogger()
|
|
{
|
|
var (logger, inner, _) = Make();
|
|
|
|
logger.BeginScope("state");
|
|
|
|
Assert.Equal(["state"], inner.Scopes);
|
|
}
|
|
}
|