liveBook · Manning

Logger

As explained in ASP.NET Core in Action, 3e

ILogger is a .NET logging abstraction whose methods record application diagnostic messages through configured providers.

ILogger is the .NET logging abstraction for recording diagnostic messages. It lets application code report events without knowing whether logs go to the console, a file, or an external service. section 26.0 section 26.1.2

A logger carries several useful parts of a diagnostic record:

  • Log level expresses importance, ranging from Trace to Critical. section 26.2 section 26.6
  • Category identifies the component that created the message and supports filtering; ILogger<T> normally uses T's fully qualified name. section 26.2.2
  • Message templates use named placeholders such as {RecipeCount}, preserving values for structured analysis. section 26.2 section 26.6

Injecting a logger into a Razor Page

Inject ILogger<IndexModel> through dependency injection, load the recipes, and call LogInformation("Loaded {RecipeCount} recipes", Recipes.Count). The configured providers receive the message, while RecipeCount remains available as a named value. section 26.2

Figure 26.3 The components of the ASP.NET Core logging framework. You register logging providers with an ILoggerFactory, which creates implementations of ILogger. You write logs to the ILogger, which delegates to the ILogger implementations that write logs to the console or a file. You can send logs to multiple locations with this design without having to configure the locations when you create a log message.
CH26_F03_Lock3

Follow the provider-to-factory-to-logger flow to see how one ILogger call can write to multiple destinations without changing application code. section 26.1.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains injection, providers, levels, categories, templates, and structured values, while deferring detailed filtering and provider configuration.
Examples
Uses a Razor Page recipe count, console provider setup, custom categories, and a generated LoggerMessage method.
What you can do after
Apply it via ASP.NET Core dependency injection to write categorized, leveled, structured messages through configured providers.
How it connects
Connects dependency injection, ILoggerFactory, ILoggerProvider, log levels, categories, structured logging, and logging configuration.

Also explained in

Compare how 5 books explain Logger →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.