This may be somewhat related to Pass ILogger or ILoggerFactory to constructors in AspNet Core?, however this is specifically about Library Design, not about how the actual application that uses those libraries implement its logging.
I am writing a .net Standard 2.0 Library that will be installed via Nuget, and to allow people using that Library to get some debug info, I'm depending on Microsoft.Extensions.Logging.Abstractions to allow a standardized Logger to be injected.
However, I'm seeing multiple interfaces, and sample code on the web sometimes uses ILoggerFactory
and creates a logger in the ctor of the class. There's also ILoggerProvider
which looks like a read-only version of the Factory, but implementations may or may not implement both interfaces, so I'd have to pick. (Factory seems more common than Provider).
Some code I've seen uses the non-generic ILogger
interface and might even share one instance of the same logger, and some take an ILogger<T>
in their ctor and expect the DI container to support open generic types or explicit registration of each and every ILogger<T>
variation my library uses.
Right now, I do think that ILogger<T>
is the right approach, and maybe a ctor that doesn't take that argument and just passes a Null Logger instead. That way, if no logging is needed, none is used. However, some DI containers pick the largest ctor and thus would fail anyway.
I'm curious of what I'm supposed to be doing here to create the least amount of headache for users, while still allowing proper logging support if desired.