using Microsoft.Extensions.Logging;
using ZB.MOM.WW.OtOpcUa.Driver.TwinCAT;
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Tests;
/// Collects the driver's Warning-level messages (thread-safe) for once-per-X assertions.
internal sealed class TwinCATCapturingLogger : ILogger
{
private readonly List _warnings = [];
/// Gets a snapshot of the formatted Warning messages logged so far.
public IReadOnlyList Warnings { get { lock (_warnings) return [.. _warnings]; } }
///
/// Optional hook run synchronously on the logging thread for each Warning, after it is captured. A test can
/// block in it to hold open the window between the driver's log call and its next statement.
///
public Action? OnWarning { get; init; }
///
public IDisposable? BeginScope(TState state) where TState : notnull => null;
///
public bool IsEnabled(LogLevel logLevel) => true;
///
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func formatter)
{
if (logLevel != LogLevel.Warning) return;
var message = formatter(state, exception);
lock (_warnings) _warnings.Add(message);
OnWarning?.Invoke(message);
}
}