ILogger is a .NET logging abstraction for recording categorized application events through built-in or third-party logging providers. Log entries can help track user interactions, state changes, internal activity, and exceptions. section 7.0 section 7.2
In an ASP.NET Core controller, ILogger<T> uses the type T to determine the log category. The logger is commonly supplied through dependency injection; for example, a board-game controller can receive ILogger<BoardGamesController> in its constructor. section 7.2
public BoardGamesController(
ApplicationDbContext context,
ILogger<BoardGamesController> logger)
{
_context = context;
_logger = logger;
}
A controller constructor receives its logger through dependency injection and stores it for use in controller methods. section 7.2
Useful ILogger conventions include:
- Choose a log level, such as Information, to describe the event. section 7.2.1
- Use event IDs to identify recurring events; constants can make those IDs clearer and reduce mistyped values. section 7.2.5
- Use named message-template placeholders to make log messages easier to read and associate values with the template. section 7.2.5
A controller can log that its Get method has started, then verify the result in the development Output window. The entry shows the controller category, level, and message. Other components can use the same logging API, so configuration changes can affect their logs too. section 7.2.1
Serilog can serve as a provider for the Microsoft logging API rather than replacing it. Its optional enrichers add event context, while sinks send events to destinations such as databases or cloud repositories. section 7.4.1




