Will ILogger <T> introduce a new journal each time?
The registration examples in the documentation provide an example of how to insert a registrar into a controller:
public class TodoController : Controller
{
private readonly ITodoRepository _todoRepository;
private readonly ILogger _logger;
public TodoController(ITodoRepository todoRepository,
ILogger<TodoController> logger)
{
_todoRepository = todoRepository;
_logger = logger;
}
}
Is the DI environment creating a new registrar every time I introduce a registrar into something like this? Is there a better way?
+4
1 answer
It is easy to answer an external source. When you execute services.AddLogging()
, the default behavior is that it is ILogger<T>
registered as a singleton :
public static IServiceCollection AddLogging(this IServiceCollection services, Action<ILoggingBuilder> configure)
{
// …
services.TryAdd(ServiceDescriptor.Singleton<ILoggerFactory, LoggerFactory>());
services.TryAdd(ServiceDescriptor.Singleton(typeof(ILogger<>), typeof(Logger<>)));
// …
}
, ILogger<T>
T
, . , ILogger<TodoController>
, .
, , . DI, . , ; .
, ? . , ( ), - ILogger<T>
T
, . Theres , , , , ;)
+6