How does logging work in ASP.NET Core?

ASP.NET Core Logging Basic 1–2 Yrs2–3 Yrs Conceptual 1 min read

Quick Answer

Logging is built in: inject ILogger<T> and call _logger.LogInformation(...). It's provider-based — Console, Debug, EventSource by default, plus Serilog, Seq, Application Insights, etc. Log levels (Trace → Critical) are filtered per category via configuration.

It's built into the framework. I inject ILogger<MyClass> and log through it, and behind the scenes there are providers — console, debug, and then whatever you add like Serilog or Application Insights. The important habit is structured logging: I write LogInformation("Order {OrderId} placed", id) with named placeholders rather than string interpolation, so the log store captures OrderId as a real field I can filter on. Levels are configured per namespace in appsettings, and scopes let me attach a correlation id to every line for a request.

  • Structured logging: _logger.LogInformation("Order {OrderId} placed by {UserId}", id, userId) — the placeholders become named properties, not just string interpolation, so you can query OrderId in Seq/Elasticsearch.
  • Levels & filtering: set minimum level per namespace in appsettings.json ("Microsoft.AspNetCore": "Warning").
  • Scopes: using (_logger.BeginScope(...)) attaches context (a correlation id) to every log line inside.
  • Performance: use the message template, not $"..."; source-generated LoggerMessage for hot paths.