liveBook · Manning

Logger

As explained in Building Web APIs with ASP.NET Core

ILogger is a .NET logging abstraction for writing categorized diagnostic messages.

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

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains generic categories, dependency injection, log levels, event IDs, and message templates through controller examples.
Examples
Uses a board-game controller log entry, event ID constants, named placeholders, and Output-window results.
What you can do after
Apply it via ASP.NET Core dependency injection to record categorized controller events and inspect their output.
How it connects
Connects logging providers with dependency injection, log levels, event IDs, message templates, and Serilog.

Also explained in

Compare how 5 books explain Logger →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.