Skip to content

Instantly share code, notes, and snippets.

@gsoulavy
Last active October 13, 2021 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsoulavy/56dda58e69e0b16293bed4eb6a6095c5 to your computer and use it in GitHub Desktop.
Save gsoulavy/56dda58e69e0b16293bed4eb6a6095c5 to your computer and use it in GitHub Desktop.
MockLogger for assertions of Microsoft.Extensions.Logger
public abstract class MockLogger<T> : ILogger<T>
{
void ILogger.Log<TState>(
LogLevel logLevel,
EventId eventId,
TState state,
Exception exception,
Func<TState, Exception, string> formatter)
=> Log(logLevel, formatter(state, exception));
public abstract void Log(LogLevel logLevel, string message);
public virtual bool IsEnabled(LogLevel logLevel) => true;
public abstract IDisposable BeginScope<TState>(TState state);
}
// Asserts
/*
MockLogger<T> logger = Substitute.For<MockLogger<T>>();
new UnderTest(logger).DoSomething();
logger.Received().Log(Arg.Any<LogLevel>(), Arg.Is<string>(s => s.Contains("some log message")));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment