The MS docs article "Introduction to Logging in ASP.NET Core" gives 2 examples of constructor injection
using ILogger
private readonly ILogger _logger; public TodoController(ILogger<TodoController> logger) { _logger = logger; }
and ILoggerFactory
private readonly ILogger _logger; public TodoController( ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger<TodoController>(); }
My question is what should I pass to child classes called from my controller
pass ILoggerFactory to my child classes called from the controller and in each class call
LoggerFactoryExtensions.CreateLogger<MyChildClass>()
orpass parent controller's
ILogger<MyController>
to each child class created from the controller and having non-generic parameter ILogger.
In logs I prefer to see separate category 'MyChildClass' for each class, rather than all classes use the category 'MyController' from the parent controller.
However CreateLogger in each object construction can be an expensive operation (e.g. see https://github.com/aspnet/Logging/issues/524)
Which option will you recommend? Can you suggest any other approach?