liveBook · Manning

ILoggerProvider

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

ILoggerProvider is a logging abstraction that creates logger instances configured to write messages to a particular destination or system.

An ILoggerProvider determines where ASP.NET Core log messages go. A console provider writes to the console; a file provider writes to a file. Your application writes messages through ILogger rather than calling a provider directly, so you can change destinations without changing the code that creates each message. section 26.1.2 section 26.3

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

The diagram shows how ILoggerFactory connects configured providers to the ILogger used by application code. One log call can reach multiple destinations. section 26.1.2

WebApplicationBuilder already adds console and debug providers. To add another destination, register another provider through builder.Logging. ILoggerFactory then creates a logger that sends eligible messages to each configured provider. section 26.3 section 26.1.2

Keep console logs and add a file

Install NetEscapades.Extensions.Logging.RollingFile with `dotnet add package NetEscapades.Extensions.Logging.RollingFile`, then call `builder.Logging.AddFile()` after `WebApplication.CreateBuilder(args)`. The file provider is added alongside the existing providers; code that uses ILogger does not need to change. section 26.3

Providers can receive different subsets of messages. Filtering considers the log level, logger category, and provider; the most specific applicable rule decides whether a provider writes a message. For example, you can keep detailed file logs while requiring Error-level messages on the console. This helps reduce production noise without removing useful development detail. section 26.4

Destination is not the only choice: providers also differ in how they handle log data. A message such as `"Loaded {RecipeCount} recipes"` supplies a named value that some providers can retain for searching. The default console format displays the completed message but does not make that value searchable by key; JSON console output or a structured logging provider can preserve more structure. Providers also decide how to handle data attached through logging scopes. section 26.2.3 section 26.5.2

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

How deep it goes
Explains provider creation through ILoggerFactory, registration, filtering, and structured output, but does not implement a provider.
Examples
Uses console and file destinations, a third-party AddFile() registration, and provider-specific filtering rules.
What it assumes
Starts from a small ASP.NET Core application and introduces the ILogger and dependency-injection relationships needed to use providers.
What you can do after
Operate/configure it with WebApplicationBuilder.Logging and filtering configuration to add destinations and control their verbosity.
How it connects
Connects ILoggerProvider to ILogger, ILoggerFactory, dependency injection, log filtering, structured logging, and logging scopes.

Also explained in

Compare how 2 books explain ILoggerProvider →

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.