Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 1 KB

File metadata and controls

32 lines (25 loc) · 1 KB

Meziantou.Extensions.Logging.InMemory

using var loggerProvider = new InMemoryLoggerProvider();
var logger = loggerProvider.CreateLogger("MyLogger");

// do stuff with the logger

// Assert
Assert.Empty(loggerProvider.Logs.Errors);
Assert.Single(loggerProvider.Logs, log => log.Message.Contains("test") && log.EventId.Id == 1);

If you are using a WebApplicationFactory:

using var loggerProvider = new InMemoryLoggerProvider();
using var factory = new WebApplicationFactory<Program>()
    .WithWebHostBuilder(builder =>
    {
        builder.ConfigureLogging(builder =>
        {
            // You can override the logging configuration if needed
            //builder.SetMinimumLevel(LogLevel.Trace);
            //builder.AddFilter(_ => true);

            builder.Services.AddSingleton<ILoggerProvider>(loggerProvider);
        });
    });

Blog post about testing logging: How to test the logs from ILogger in .NET