Skip to content

Microsoft.Extensions.Logging compatible logger for recording log messages during tests

License

Notifications You must be signed in to change notification settings

horsdal/InMemoryLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status NuGet version

InMemoryLogger

Microsoft.Extensions.Logging compatible logger for recording log messages during tests

Getting started

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.

About

Microsoft.Extensions.Logging compatible logger for recording log messages during tests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages