Created
April 14, 2021 21:23
-
-
Save djeikyb/37f1e93590011b925be9cf7718ae0c83 to your computer and use it in GitHub Desktop.
the simplest possible bridge between xunit and microsoft's ilogger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.Extensions.Logging; | |
using Xunit.Abstractions; | |
public class XunitLogger<T> : ILogger<T> | |
{ | |
private readonly ITestOutputHelper _outputHelper; | |
public XunitLogger(ITestOutputHelper outputHelper) | |
{ | |
_outputHelper = outputHelper; | |
} | |
public IDisposable BeginScope<TState>(TState state) => throw new NotImplementedException(); | |
public bool IsEnabled(LogLevel logLevel) => true; | |
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, | |
Func<TState, Exception, string> formatter) => | |
_outputHelper.WriteLine($"[{logLevel}] {formatter.Invoke(state, exception)}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use in a test: