Member-only story
Unit Testing with ILogger<T>
Some common ways to work with ILogger<T>
objects in unit testing.

EDIT 11/16/2020: The code has updated to .NET 5
EDIT 04/13/2020: The code has updated to .NET Core 3.1
An effective logging system is essential for application monitoring and analysis. .NET Core supports a logging API that works with a variety of built-in and third-party logging providers, such as the Console provider, the Azure Application Insights provider, Serilog, elma.io, and so on.
In order to create logs, we need to use an ILogger<T>
object, which is readily accessible in our applications. (1) In a web application or a hosted service, we can get an ILogger<T>
object from the native .NET Core dependency injection (DI) system. (2) In a non-host console application, we can use the LoggerFactory
to create an ILogger<T>
object.
Because ILogger<T>
objects are frequently used in controllers and service classes, we cannot avoid them in unit tests. In this post, we will go over some common ways to work with ILogger<T>
objects in unit testing. The full project can be found in this repository.