Modern applications need more than just working code — they need visibility into production. ASP.NET Core now treats observability as a first-class feature, providing built-in metrics, diagnostics, logging, and tracing.
This post covers:
Built-in observability features in ASP.NET Core
Step-by-step setup
Real-world coding examples for metrics, logging, and tracing
Dashboards, alerts, and production readiness
What Is Observability and Why Does It Matter
Observability is the ability to understand the internal state of your application from external outputs like metrics, logs, and traces.
Core Observability Components
your application's internal state from external outputs such asMetrics – Quantitative data (request rate, latency, errors).
Logs – Detailed events or errors with structured information.as Metrics
Traces – End-to-end visibility of requests across services.
Proper observability ensures you can detect and diagnose issues without guessing .
Built-In Observability Features in ASP.NET Core
ASP.NET Core now ships with rich diagnostics:
HTTP request counts and durations
Active requests
Failed requests (4xx / 5xx)
Connection, TLS, and runtime metrics (CPU, memory, GC)
These features reduce the need for heavy custom instrumentation and integrate easily with Prometheus, Grafana, and Azure Monitor.
Step-by-Step Setup of Observability
1. Create or Open Your ASP.NET Core App
dotnet new webapi -n ObservabilityDemo
cd ObservabilityDemo 2. Add Required NuGet Packages
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Extensions.Hosting
dotnet add package OpenTelemetry.Instrumentation.AspNetCore
dotnet add package OpenTelemetry.Exporter.Prometheus.AspNetCore
3. Configure OpenTelemetry in Program.cs
using OpenTelemetry.Metrics;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics
.AddAspNetCoreInstrumentation()
.AddRuntimeInstrumentation()
.AddPrometheusExporter();
});
var app = builder.Build();
app.MapPrometheusScrapingEndpoint();
app.MapControllers();
app.Run();
Your app now exposes production-ready metrics at
/metrics.
Real-World Coding Examples
These examples illustrate practical observability in production scenarios.
Example 1: Track API Usage (Business Metric)
using System.Diagnostics.Metrics;
public static class AppMetrics
{
public static readonly Meter Meter = new("ObservabilityDemo");
public static readonly Counter<int> OrdersCreated =
Meter.CreateCounter<int>("orders_created");
}
[ApiController]
[Route("api/orders")]
public class OrdersController : ControllerBase
{
[HttpPost]
public IActionResult CreateOrder()
{
AppMetrics.OrdersCreated.Add(1);
return Ok("Order created successfully");
}
}
Scenario: Track how many orders your API receives per minute for business insight.
Join the conversation! Your thoughts help the community grow.