c810bd0f7c
#120: Akka ILoggingAdapter lines used to bypass Serilog and go to stdout only. - BuildHocon sets akka.loggers = [Host.Actors.SerilogAkkaLogger] and keeps akka.loglevel = INFO, so Akka Debug chatter is still filtered at the adapter. The INFO+ lines that used to reach stdout now go through the Serilog pipeline instead, with no extra volume. - Lines reach the file sinks with the node enrichers. SourceContext is the logging class's full name, AkkaSource is the actor path, Akka's own timestamp is kept, and format arguments become structured properties. - The Akka logger actor writes on its own thread, after the logging actor's LogContext is gone. To keep the ExecutionId, the two DCL actors log through ExecutionAwareLogging.GetLogger(Context), which records ExecutionLogContext.Current (a new AsyncLocal mirror of the pushed id) in an ExecutionTaggedLogMessage. The logger actor pushes that id again while it writes the line. #56 remainder (coordinator decision: no replicated schema change): - The host pipeline gains ExecutionLogCaptureSink. - While a run is traced, its step logger registers its ExecutionId: the site ScriptLogger from the launcher (plus each inline CallShared child id), and the central InboundScriptLogger from the executor. - Each tagged line becomes a ScriptLog row with LogSource "node" and the new ScriptTraceExtra.LogCategory. - The rows count against the existing MaxLogLinesPerRun cap, are clipped at 2 000 chars, ride the low-priority collector lane and pass the same redactor. Untraced runs cost one volatile read. - Lines are never copied twice. The step loggers' own forwards run inside ExecutionLogCapture.Suppress(), and the sink suppresses itself while it calls a target. The registration is declared inside the launcher's and executor's try, so it ends before the catch blocks log the outcome, and FlushDropped closes it. - FlowGraphBuilder lists node lines in FlowGraph.NodeLogs and never folds them onto a step. - The Flow Debugger shows them in a Node log card (FlowNodeLogPanel): grouped per execution under its run step, with an All | Warn + filter. Live and Replay both read the same rows, so site scope and role rules apply unchanged. - CLI --show-logs prints them as a trailing block. Tests: Commons ExecutionLogCaptureTests, ExecutionLogContext.Current, FlowGraphBuilder node lines. SiteRuntime NodeLogCaptureTests covers cross-actor capture via the launcher, untraced runs capturing nothing, no double capture of script and runtime lines or of the launcher failure line, the shared cap, collector redaction, shared-script child ids and FlushDropped closing capture. InboundNodeLogCaptureTests covers the central path. DCL ExecutionAwareLoggingTests covers the DataConnectionActor write-path Debug line being tagged and an uncorrelated line not being tagged. Host SerilogAkkaLoggerTests covers mapping, the ExecutionId crossing the logger hop, HOCON, ActorSystem boot and the Akka line reaching a traced run's step log end to end. Host LoggerConfigurationTests checks that the sink is wired. bUnit FlowNodeLogPanelTests and a CLI projection test cover the UI and CLI. Every new assertion was mutation-proven. Docs: step-logs design section 12, Component-SiteRuntime, CentralUI, InboundAPI, Host, CLI README, CLAUDE.md. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
87 lines
3.8 KiB
C#
87 lines
3.8 KiB
C#
using Serilog.Context;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.Commons.Observability;
|
|
|
|
/// <summary>
|
|
/// Stamps an execution id onto the Serilog <c>LogContext</c> (ScadaBridge#56), so every line
|
|
/// logged inside the scope - through Serilog directly or through a Microsoft
|
|
/// <c>ILogger</c> backed by Serilog (<c>UseSerilog</c>) - carries an <c>ExecutionId</c>
|
|
/// property once the host enables <c>Enrich.FromLogContext()</c>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <para><b>Why per message.</b> <c>LogContext</c> is an <c>AsyncLocal</c>: it follows one
|
|
/// async call chain but does not cross an Akka message hop (the dispatcher does not flow the
|
|
/// sender's execution context into the receiver). An actor that serves an execution therefore
|
|
/// re-establishes the id from the message itself, for the duration of that one message:
|
|
/// <code>
|
|
/// protected override bool AroundReceive(Receive receive, object message)
|
|
/// {
|
|
/// using (ExecutionLogContext.PushFor(message))
|
|
/// return base.AroundReceive(receive, message);
|
|
/// }
|
|
/// </code>
|
|
/// Continuations the handler starts (<c>ContinueWith</c>, <c>PipeTo</c>, an adapter call)
|
|
/// capture the context at creation, so their lines carry the id too.</para>
|
|
/// <para><b>Cost.</b> A message that is not <see cref="IExecutionCorrelated"/>, or names no
|
|
/// execution, costs one type test and allocates nothing.</para>
|
|
/// <para><b>Nesting.</b> A later push of the same property wins, so a child run started while
|
|
/// serving its parent's message logs under its OWN id (the launcher pushes it).</para>
|
|
/// </remarks>
|
|
public static class ExecutionLogContext
|
|
{
|
|
/// <summary>The log-event property carrying the execution id.</summary>
|
|
public const string ExecutionIdProperty = "ExecutionId";
|
|
|
|
/// <summary>
|
|
/// The innermost execution id pushed on this async flow, or null. Mirrors the
|
|
/// <see cref="ExecutionIdProperty"/> on the Serilog <c>LogContext</c>, which cannot be read back.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// ScadaBridge#120: Akka's <c>ILoggingAdapter</c> hands each line to a logger actor on another
|
|
/// thread, where this flow's <c>LogContext</c> is gone. An adapter that captures this value when
|
|
/// the line is logged lets the logger actor re-push it when it writes the line to Serilog.
|
|
/// </remarks>
|
|
public static Guid? Current => CurrentId.Value;
|
|
|
|
private static readonly AsyncLocal<Guid?> CurrentId = new();
|
|
|
|
/// <summary>Pushes <paramref name="executionId"/> as <see cref="ExecutionIdProperty"/>.</summary>
|
|
/// <param name="executionId">The execution id; null pushes nothing.</param>
|
|
/// <returns>A disposable that pops the property, or null when nothing was pushed.</returns>
|
|
public static IDisposable? Push(Guid? executionId)
|
|
{
|
|
if (executionId is not { } id)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var previous = CurrentId.Value;
|
|
var property = LogContext.PushProperty(ExecutionIdProperty, id);
|
|
CurrentId.Value = id;
|
|
return new Scope(property, previous);
|
|
}
|
|
|
|
/// <summary>Pushes the execution a message serves, when it names one.</summary>
|
|
/// <param name="message">Any actor message.</param>
|
|
/// <returns>A disposable that pops the property, or null when the message names no execution.</returns>
|
|
public static IDisposable? PushFor(object? message)
|
|
=> message is IExecutionCorrelated correlated ? Push(correlated.CorrelatedExecutionId) : null;
|
|
|
|
/// <summary>Pops the pushed property and restores the previous <see cref="Current"/>.</summary>
|
|
private sealed class Scope(IDisposable property, Guid? previous) : IDisposable
|
|
{
|
|
private int _disposed;
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Interlocked.Exchange(ref _disposed, 1) != 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
CurrentId.Value = previous;
|
|
property.Dispose();
|
|
}
|
|
}
|
|
}
|