feat(scripting): ScriptLogTopicSink — script LogEvent → ScriptLogEntry → publisher

This commit is contained in:
Joseph Doherty
2026-06-10 11:38:54 -04:00
parent 12423899aa
commit 14fe88fc80
5 changed files with 292 additions and 0 deletions
@@ -0,0 +1,19 @@
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging;
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
/// <summary>
/// Abstraction over the mechanism that publishes <see cref="ScriptLogEntry"/> records
/// to a downstream consumer (e.g. an Akka DPS <c>script-logs</c> topic). Implemented by
/// the concrete DPS publisher in a later task; the sink depends only on this interface
/// so the Core.Scripting layer has no Akka dependency.
/// </summary>
public interface IScriptLogPublisher
{
/// <summary>
/// Publish a single <see cref="ScriptLogEntry"/>. Implementations must be
/// thread-safe — Serilog sinks may be called from any thread.
/// </summary>
/// <param name="entry">The entry to publish. Must not be <c>null</c>.</param>
void Publish(ScriptLogEntry entry);
}
@@ -0,0 +1,70 @@
using Serilog.Core;
using Serilog.Events;
using ZB.MOM.WW.OtOpcUa.Commons.Messages.Logging;
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
/// <summary>
/// Serilog sink that converts each script <see cref="LogEvent"/> into a
/// <see cref="ScriptLogEntry"/> and forwards it to an <see cref="IScriptLogPublisher"/>.
/// The publisher implementation (supplied by a later task) routes the entry onto the
/// Akka DPS <c>script-logs</c> topic so the live Script-log Admin UI page can display it.
/// </summary>
/// <remarks>
/// <para>
/// Expected to be registered in the root script-logger pipeline alongside the
/// rolling <c>scripts-*.log</c> file sink and the
/// <see cref="ScriptLogCompanionSink"/>. Events below the configured minimum level
/// are silently discarded so Debug/Verbose noise does not saturate the cluster bus
/// in production deployments.
/// </para>
/// <para>
/// Identity properties (<c>ScriptId</c>, <c>VirtualTagId</c>, <c>AlarmId</c>,
/// <c>EquipmentId</c>) are lifted from the event's structured-property bag. If
/// <c>ScriptId</c> is absent the sink falls back to the legacy <c>ScriptName</c>
/// property so pipelines that pre-date this sink continue to work correctly. If
/// neither property is present <c>ScriptId</c> defaults to <c>"unknown"</c>.
/// </para>
/// </remarks>
public sealed class ScriptLogTopicSink : ILogEventSink
{
private readonly IScriptLogPublisher _publisher;
private readonly LogEventLevel _min;
/// <summary>Initializes a new instance of the <see cref="ScriptLogTopicSink"/> class.</summary>
/// <param name="publisher">The publisher that routes entries to the cluster bus. Must not be <c>null</c>.</param>
/// <param name="min">Minimum log level to forward. Events below this level are discarded (default: <see cref="LogEventLevel.Information"/>).</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="publisher"/> is <c>null</c>.</exception>
public ScriptLogTopicSink(IScriptLogPublisher publisher, LogEventLevel min = LogEventLevel.Information)
{
_publisher = publisher ?? throw new ArgumentNullException(nameof(publisher));
_min = min;
}
/// <summary>
/// Converts the <paramref name="logEvent"/> to a <see cref="ScriptLogEntry"/> and
/// publishes it, unless the event is below the configured minimum level.
/// </summary>
/// <param name="logEvent">The Serilog event to process. Silently ignored when <c>null</c>.</param>
public void Emit(LogEvent logEvent)
{
if (logEvent is null || logEvent.Level < _min) return;
// Extract a string scalar property by key, returning null when absent or non-string.
string? P(string key) =>
logEvent.Properties.TryGetValue(key, out var v) && v is ScalarValue { Value: string s }
? s
: null;
_publisher.Publish(new ScriptLogEntry(
ScriptId: P(ScriptLoggerFactory.ScriptIdProperty)
?? P(ScriptLoggerFactory.ScriptNameProperty)
?? "unknown",
Level: logEvent.Level.ToString(),
Message: logEvent.RenderMessage(),
TimestampUtc: logEvent.Timestamp.UtcDateTime,
VirtualTagId: P(ScriptLoggerFactory.VirtualTagIdProperty),
AlarmId: P(ScriptLoggerFactory.AlarmIdProperty),
EquipmentId: P(ScriptLoggerFactory.EquipmentIdProperty)));
}
}
@@ -28,6 +28,18 @@ public sealed class ScriptLoggerFactory
/// <summary>Structured property name the enricher binds. Stable for log filtering.</summary>
public const string ScriptNameProperty = "ScriptName";
/// <summary>Structured property name carrying the Script row identifier (<c>Script.ScriptId</c>).</summary>
public const string ScriptIdProperty = "ScriptId";
/// <summary>Structured property name carrying the VirtualTag identifier, when the script runs in a virtual-tag context.</summary>
public const string VirtualTagIdProperty = "VirtualTagId";
/// <summary>Structured property name carrying the ScriptedAlarm identifier, when the script runs in an alarm context.</summary>
public const string AlarmIdProperty = "AlarmId";
/// <summary>Structured property name carrying the Equipment identifier for per-equipment script evaluations.</summary>
public const string EquipmentIdProperty = "EquipmentId";
private readonly ILogger _rootLogger;
/// <summary>Initializes a new instance of the <see cref="ScriptLoggerFactory"/> class.</summary>
@@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZB.MOM.WW.OtOpcUa.Commons\ZB.MOM.WW.OtOpcUa.Commons.csproj"/>
<ProjectReference Include="..\ZB.MOM.WW.OtOpcUa.Core.Abstractions\ZB.MOM.WW.OtOpcUa.Core.Abstractions.csproj"/>
<ProjectReference Include="..\ZB.MOM.WW.OtOpcUa.Core.Scripting.Abstractions\ZB.MOM.WW.OtOpcUa.Core.Scripting.Abstractions.csproj"/>
</ItemGroup>