using Microsoft.Extensions.Logging; namespace Bifrost.Web.Tests.Support; public sealed class CapturingLoggerProvider : ILoggerProvider { private readonly List _lines = []; private readonly List _warnings = []; /// Lines logged at Warning or above, to pin that an operator-facing notice reaches the journal. public IReadOnlyList WarningLines { get { lock (_lines) { return _warnings.ToArray(); } } } public IReadOnlyList Lines { get { lock (_lines) { return _lines.ToArray(); } } } public ILogger CreateLogger(string categoryName) => new Capture(this); public void Dispose() { } private sealed class Capture(CapturingLoggerProvider owner) : 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) { lock (owner._lines) { var line = formatter(state, exception) + (exception is null ? string.Empty : "\n" + exception); owner._lines.Add(line); if (logLevel >= LogLevel.Warning) { owner._warnings.Add(line); } } } } }