Logging adds the following services to DI
services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
and Logger<>
depends on ILoggerFactory
from DI.
For your second scenario you would need to inject ILoggerFactory instead of ILogger.
public Foo(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger("logger name here");
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…