Files
caliverse_server/BrokerApiTest/Helper/TestLogger.cs
2025-05-01 07:20:41 +09:00

38 lines
853 B
C#

namespace BrokerTest.Helper;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using Xunit.Abstractions;
public class TestLogger<T> : ILogger<T>, IDisposable
{
private readonly List<string> m_logs = [];
private readonly ITestOutputHelper m_output;
public TestLogger(ITestOutputHelper output)
{
m_output = output;
}
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return null;
}
public bool IsEnabled(LogLevel logLevel) => true;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
var message = formatter(state, exception);
m_logs.Add(message);
m_output.WriteLine(message);
}
public IEnumerable<string> Logs => m_logs;
public void Dispose() => m_logs.Clear();
}