Microsoft.Extensions.Logging compatible logger for recording log messages during tests
Install from NuGet:
> dotnet add package InMemoryLogger
Then add InMemoryLogger as the logger in your applications service collection:
var services = new ServiceCollection()
.AddLogging(x => x.AddInMemory())
.BuildServiceProvider();
var inMemLogger = services.GetService<InMemoryLogger>();
And then write a test that asserts on logs messages:
var logger = services.GetService<ILogger<MyTest>>();
logger.LogWarning("This is a log message");
Assert.Contains(inMemLogger.RecordedWarningLogs, l => l.Message == "This is a log message");
Or on logged exceptions:
var logger = services.GetService<ILogger<MyTest>>();
var expected = new Exception();
logger.LogError(expected, "This is another log message");
Assert.Contains(inMemLogger.RecordedErrorLogs, l => l.Exception == expected);
That's it. All logs are in memory, all logs are recorded.