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