File: RecordingLogger.cs
Web Access
Project: src\tests\Aspire.Hosting.RemoteHost.Tests\Aspire.Hosting.RemoteHost.Tests.csproj (Aspire.Hosting.RemoteHost.Tests)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
 
namespace Aspire.Hosting.RemoteHost.Tests;
 
/// <summary>
/// Minimal in-memory logger used by tests that need to inspect emitted log entries.
/// Thread-safe; entries are appended in the order Log is called.
/// </summary>
internal sealed class RecordingLogger<T> : ILogger<T>
{
    public ConcurrentQueue<LogEntry> Entries { get; } = new();
 
    public IDisposable BeginScope<TState>(TState state) where TState : notnull => NullScope.Instance;
 
    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(new LogEntry(logLevel, eventId, formatter(state, exception), exception));
    }
 
    internal sealed record LogEntry(LogLevel Level, EventId EventId, string Message, Exception? Exception);
 
    private sealed class NullScope : IDisposable
    {
        public static readonly NullScope Instance = new();
        public void Dispose() { }
    }
}