If class T contains dependency on ILogger, dependency is resolved:
public class Foo
{
private ILogger _logger;
public Foo(ILogger<Foo> logger)
{
_logger = logger;
}
}
but the following does not work, as logger
will be null:
public class Foo
{
private ILogger _logger;
public Foo(ILogger logger)
{
_logger = logger;
}
}
Logging adds the following services to DI
and
Logger<>
depends onILoggerFactory
from DI.For your second scenario you would need to inject ILoggerFactory instead of ILogger.
Your first scenerio works because
ILogger<>
inherits fromILogger
(see source code)But as @KiranChalla said, in the
LoggingServiceCollectionExtensions
ILogger<>
is registered(notILogger
) so your second scenerio does not work.