using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace ZB.MOM.WW.OtOpcUa.Security.Tests;
/// Collects every log entry written through the host's logger factory, for assertions on what was logged.
internal sealed class CapturingLoggerProvider : ILoggerProvider
{
public ConcurrentQueue Entries { get; } = new();
public ILogger CreateLogger(string categoryName) => new Logger(categoryName, Entries);
public void Dispose() { }
internal sealed record Entry(string Category, LogLevel Level, string Message, Exception? Exception);
private sealed class Logger(string category, ConcurrentQueue entries) : ILogger
{
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) =>
entries.Enqueue(new Entry(category, logLevel, formatter(state, exception), exception));
}
}