Two C# Unit Test Tips: NullLogger and FakeTimeProvider

Logging Unless you are actually testing your logging (which I mostly wouldn’t recommend), you don’t need to mock your own loggers. Microsoft has you covered with the NullLogger Class. It is exactly what it sounds like: A mocked logger that can be injected where an ILogger is required, which doesn’t log anything. Here is how to use it: // Class which requires a logger class LicensePlateDetector(ILogger logger) { // (...) } // Instantiating the class in the unit test LicensePlateDetector detector = new(NullLogger.Instance); // Or if you need ILogger<T> LicensePlateDetector detector = new(NullLogger<LicensePlateDetector>.Instance); I have seen suggestions to default to NullLogger in production code using the ?? operator. I would not recommend this, as you risk silent failures if you forget to inject the actual logger. ...

March 8, 2026 · 3 min · Jakob Busk Sørensen

Background Processing With Channels

I recently came across a scenario, where I had to process a lot of incoming data in real time, where missing a single entry is not an option. At the same time I wanted to disconnect the class which was fetching data (the collector) from the class which were processing the data (the processor). The result was a combination of Channels and the BackgroundService. Before we look at the code, you will need the Microsoft.Extensions.Hosting Nuget package. You can install it with: ...

April 15, 2025 · 3 min · Jakob Busk Sørensen