ASP.NET Core Interview Question #152
What is ILogger<T> in ASP.NET Core?
Logging, Error Handling & Background Services Mid-Level Intermediate
Quick Interview Answer
ILogger<T> is a typed logging abstraction provided through dependency injection. The generic type normally identifies the class producing the log and becomes the logging category.
Detailed Explanation
ILogger<T> is a typed logging abstraction provided through dependency injection. The generic type normally identifies the class producing the log and becomes the logging category.
Using ILogger<T> allows logging configuration to filter messages by category and makes the source of log events easier to identify.
The logger can be constructor-injected into controllers, services, background workers, middleware, and other DI-created components.
Code Example
public class ProductsController : Controller
{
private readonly ILogger<ProductsController> _logger;
public ProductsController(
ILogger<ProductsController> logger)
{
_logger = logger;
}
}