Use this fake logger when required within unit tests, where you are using Microsoft Logging and directly injecting the logger
public class FakeLogger<T> : ILogger<T>
{
private sealed class NoOpDisposable : IDisposable
{
public void Dispose()
{
}
}
public IDisposable BeginScope<TState>(TState state) where TState : notnull
{
return new NoOpDisposable();
}
public bool IsEnabled(LogLevel logLevel)
{
return true;
}
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception?, string> formatter)
{
string message = "";
if (formatter != null)
{
message += formatter(state, exception);
}
var logLeveChar = logLevel.ToString()[0];
Console.WriteLine($"{logLeveChar} - {typeof(T)} - {message}");
}
}
No comments:
Post a Comment