2026-07-24 09:22:17 +08:00

26 lines
924 B
C#

using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
namespace BuildFlow.Monitoring.Tests.Sync;
/// <summary>记录结构化日志格式化后的级别和文本,供安全断言使用。</summary>
public sealed class RecordingLogger<T> : ILogger<T>
{
private readonly ConcurrentQueue<(LogLevel Level, string Message)> _entries = new();
/// <summary>当前已记录的所有日志。</summary>
public IReadOnlyCollection<(LogLevel Level, string Message)> Entries => _entries.ToArray();
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
=> _entries.Enqueue((logLevel, formatter(state, exception)));
}