liveBook · Manning

Logging level

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

The Information log level records normal application flow and contextual events that help explain subsequent behavior.

A logging level says how important a message is. Use Information for normal application flow, such as a user viewing a page. These messages can provide context for understanding what happened before an error. Every message created with ILogger has a level, and that level also helps determine whether a logging provider writes it. section 26.2.1

Choose a level that matches the event, from most to least serious:

  • Critical: a disastrous failure that may prevent the app from functioning correctly. section 26.2.1
  • Error: an operation fails, but the app can continue serving other requests. section 26.2.1
  • Warning: an unexpected condition the app can work around, such as a missing requested item. section 26.2.1
  • Information: normal actions that help trace application behavior. section 26.2.1
  • Debug and Trace: increasingly detailed messages; Trace may contain sensitive information. section 26.2.1

Log a normal event

In a Razor Page handler, call _log.LogInformation("Loaded {RecipeCount} recipes", Recipes.Count) after loading recipes. Inject ILogger<IndexModel> into the page model to obtain the logger. The named placeholder supplies the displayed count and preserves a value useful for structured logging; prefer it to string interpolation. section 26.2

Listing 26.3 Specifying the log level using extension methods on ILogger
private readonly ILogger _log;                                  #1
public async IActionResult OnGet(int id)
{
    _log.LogInformation(                                        #2
        "Loading recipe with id {RecipeId}", id);               #2
 
    Recipe = _service.GetRecipeDetail(id);
    if (Recipe is null)
    {
        _log.LogWarning(                                        #3
            "Could not find recipe with id {RecipeId}", id);    #3
        return NotFound();
    }
    return Page();
}
#1 An ILogger instance is injected into the Razor Page using constructor injection.
#2 Writes an Information level log message
#3 Writes a Warning level log message

Compare an Information message when a recipe is requested with a Warning message when that recipe cannot be found. section 26.2.1

A message’s level does not guarantee that every provider writes it. Filtering selects a minimum level using the message level, logger category, and provider. For example, a default minimum of Information can retain ordinary application messages while a Warning minimum for Microsoft categories suppresses less important framework messages. Production settings often filter more aggressively than development settings. section 26.4 section 26.6

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
      }
    }
  }
}

Use the appsettings.json example to see separate default, category, file-provider, and console-provider minimum levels. section 26.4

When diagnosing a production problem, a more verbose setting can provide extra context. File-based configuration can be set to reload on change, allowing a temporary logging change without recompiling or restarting; this reloading generally does not apply to environment-variable configuration. section 10.2.4

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

How deep it goes
Defines six severity levels, demonstrates ILogger methods, and explains category- and provider-specific minimum-level filtering.
Examples
Contrasts recipe-loading Information messages with missing-recipe Warnings and shows appsettings.json filtering rules.
What it assumes
Defines LogLevel and demonstrates constructor-injected ILogger<T> before applying filtering rules.
What you can do after
Apply it via ASP.NET Core ILogger.LogInformation() and appsettings.json filters to record normal events while controlling output verbosity.
How it connects
Connects logging levels to ILogger, logger categories, logging providers, and structured logging.

Also explained in

Compare how 9 books explain logging level →

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.