Last active
October 20, 2021 22:10
-
-
Save djeikyb/cf8f81e6917335b346534f5f072a1242 to your computer and use it in GitHub Desktop.
A barely functional logger to standard out for xunit
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; | |
namespace Example | |
{ | |
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.ToString()[..3]}] {formatter.Invoke(state, exception)}\n\n{exception}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A similar hack around xunit's determined hostility: