| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using Microsoft.Extensions.Logging;
- using Xunit.Abstractions;
- namespace Tests;
- public sealed class XUnitLoggerProvider : ILoggerProvider
- {
- private readonly ITestOutputHelper _output;
- public XUnitLoggerProvider(ITestOutputHelper output)
- {
- _output = output;
- }
- public ILogger CreateLogger(string categoryName)
- {
- return new XUnitLogger(_output, categoryName);
- }
- public void Dispose()
- {
- }
- private sealed class XUnitLogger : ILogger
- {
- private readonly string _category;
- private readonly ITestOutputHelper _output;
- public XUnitLogger(ITestOutputHelper output, string category)
- {
- _output = output;
- _category = category;
- }
- public IDisposable BeginScope<TState>(TState state)
- {
- return null!;
- }
- public bool IsEnabled(LogLevel logLevel)
- {
- return true;
- }
- public void Log<TState>(LogLevel logLevel, EventId eventId,
- TState state, Exception? exception, Func<TState, Exception?, string> formatter)
- {
- _output.WriteLine($"[{logLevel}] {_category}: {formatter(state, exception)}");
- }
- }
- }
|