liveBook · Manning

Log category

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

A log category is a string identifier associated with a logger that identifies the component or type creating a log message.

A log category is a string identifier attached to each log message. It identifies the component that created the message and helps separate application, framework, and service output. section 26.2.1 section 26.2.2

Use categories to make logging identifiable and controllable:

  • Inject ILogger<T> to use the fully qualified name of T as the category, which is the usual convention. section 26.2.2
  • Inject ILoggerFactory and call CreateLogger("RecipeApp.RecipeService") when an arbitrary category string is useful. section 26.2.2
  • Filter messages by category, log level, and provider; the most specific matching rule determines whether each provider writes the message. section 26.4

A service with a category

public class RecipeService { private readonly ILogger _log; public RecipeService(ILoggerFactory factory) { _log = factory.CreateLogger("RecipeApp.RecipeService"); } } The logger records RecipeApp.RecipeService as the category. Unless custom naming is needed, inject ILogger<RecipeService> instead; its generic type supplies the category automatically. section 26.2.2

Listing 26.7 The log filtering configuration section of appsettings.json
{
  "Logging": {
    "LogLevel": {               #1
      "Default": "Debug",           #1
      "System": "Warning",          #1
      "Microsoft": "Warning"        #1
    },
    "File": {                       #2
      "LogLevel": {                 #2
        "Default": "Information"    #2
      }
    },
    "Console": {                    #3
      "LogLevel": {                 #3
        "Default": "Debug",         #3
        "Microsoft": "Warning"      #3
      }
    }
  }
}

Inspect category-based filtering alongside default, namespace-specific, and provider-specific log-level rules. section 26.4

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

How deep it goes
Explains default type-based categories, arbitrary names, logger creation, and provider filtering without descending into provider implementation.
Examples
Uses RecipeService, ILoggerFactory.CreateLogger, ILogger<T>, namespaces, and appsettings.json filtering rules.
What you can do after
Configure it via ASP.NET Core logging APIs to identify components and control output by category.
How it connects
Connects log levels, ILogger<T>, ILoggerFactory, logging providers, namespaces, filtering, and structured logging.

Also explained in

Compare how 2 books explain log category →

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.