When is a logger a dependency?

There are some discussion about whether loggers should get injected into your classes (dependency injection) or if you should use the logger facade (like LogManager.GetLogger<MyClass>()).

The answer rather simple:

As for all classes, dependencies which is mandatory should get injected.

That means that you should inject the logger if the class will fail without it (= not deliver the expected result). If the class can work without a logger: simply use the logger facade.

  • Moby Disk

    A logger that does nothing does not violate the contract. People use DI for loggers to provide a logger that does nothing for unit testing. If it violated the contract, it would fail to compile.

    The caller should have no expectation at all of what the Write() method does. That’s what encapsulation means.