Overview
Aspire provides three types of telemetry out of the box:- Structured Logging: Rich log entries with structured data from
ILogger - Distributed Tracing: Request flows across services using
Activity - Metrics: Numeric performance counters from
MeterandInstrument<T>
OpenTelemetry SDK
The .NET OpenTelemetry SDK provides:- Collection from standard .NET APIs (
ILogger,Activity,Meter) - Automatic instrumentation for ASP.NET Core, HttpClient, and runtime
- OTLP export over gRPC or HTTP
- Extensible pipeline for processing and enriching telemetry
Service Defaults Configuration
Aspire project templates include a ServiceDefaults project that configures OpenTelemetry automatically:Extensions.cs (ServiceDefaults project)
AddServiceDefaults() at startup:
Program.cs
Environment Variables
Aspire automatically configures OpenTelemetry using standard environment variables:Service Identification
Export Configuration
OTEL_EXPORTER_OTLP_ENDPOINT to point to your monitoring infrastructure.
Structured Logging
Aspire captures allILogger output as structured OpenTelemetry logs:
Log Levels
Use appropriate log levels:| Level | When to Use | Example |
|---|---|---|
Trace | Very detailed diagnostic info | ”Entering method X with parameters Y” |
Debug | Debugging information | ”Cache hit for key ‘abc‘“ |
Information | General flow of the application | ”Processing order “ |
Warning | Unexpected but handled situations | ”Retry attempt 2 of 3” |
Error | Errors and exceptions | ”Database query failed” |
Critical | Critical failures | ”Service cannot start” |
Structured Properties
Always use structured logging instead of string interpolation:Log Scopes
Add context to related log entries:Distributed Tracing
Aspire automatically instruments HTTP calls, gRPC calls, and ASP.NET Core requests usingActivity (the .NET implementation of OpenTelemetry spans).
Automatic Instrumentation
No code changes required for basic tracing:- HTTP method and URL
- Request and response headers
- Status code
- Duration
- Errors and exceptions
Custom Spans
Add custom instrumentation for important operations:ActivitySource in service defaults:
Span Events
Add timestamped events to spans:Trace Context Propagation
Trace context is automatically propagated across HTTP calls: All spans are correlated into a single distributed trace visible in the dashboard.Metrics
Aspire collects metrics from ASP.NET Core, HttpClient, and the .NET runtime automatically.Built-in Metrics
ASP.NET Core:http.server.request.duration- Request duration histogramhttp.server.active_requests- Concurrent requestsaspnetcore.routing.match_attempts- Route matching attempts
http.client.request.duration- Outbound request durationhttp.client.active_requests- Active outbound requests
process.runtime.dotnet.gc.collections.count- GC collections by generationprocess.runtime.dotnet.gc.heap.size- Heap size by generationprocess.runtime.dotnet.monitor.lock_contention.count- Lock contentionprocess.runtime.dotnet.thread_pool.threads.count- Thread pool threadsprocess.runtime.dotnet.assemblies.count- Loaded assemblies
Custom Metrics
Create application-specific metrics:Metric Types
| Type | Purpose | Example |
|---|---|---|
Counter | Ever-increasing count | Requests processed, errors |
Histogram | Distribution of values | Request duration, order value |
ObservableGauge | Current value at observation time | Queue length, memory usage |
ObservableCounter | Counter observed at export | Total bytes allocated |
Local Development Workflow
When you run your Aspire application locally:- AppHost starts the dashboard and Developer Control Plane (DCP)
- DCP configures environment variables for each service:
OTEL_SERVICE_NAME- Resource name from AppHostOTEL_RESOURCE_ATTRIBUTES- Unique instance IDOTEL_EXPORTER_OTLP_ENDPOINT- Dashboard OTLP endpoint- Fast export intervals for responsive dashboard
- Services call
AddServiceDefaults()and configure OpenTelemetry SDK - Telemetry flows to dashboard via OTLP
- Dashboard stores telemetry in memory and displays in UI
Deployment Telemetry
In deployed environments, configureOTEL_EXPORTER_OTLP_ENDPOINT to point to your observability backend:
Azure Monitor
APPLICATIONINSIGHTS_CONNECTION_STRING environment variable.
Prometheus + Grafana
Jaeger
Datadog, New Relic, Honeycomb, etc.
All OTLP-compatible backends work with Aspire. Configure the endpoint and any required authentication headers.Health Checks and Telemetry
Aspire automatically excludes health check endpoints from tracing to reduce noise:Non-.NET Applications
OpenTelemetry is language-agnostic. Configure containers and executables with OTLP environment variables:Best Practices
Always use structured logging
Always use structured logging
Use template parameters, not string interpolation, to preserve structure:
Add custom spans for business operations
Add custom spans for business operations
Trace important operations beyond HTTP calls:
Use appropriate log levels
Use appropriate log levels
Don’t over-log at
Information level. Use Debug and Trace for detailed diagnostics.Add dimensions to metrics
Add dimensions to metrics
Include relevant tags for filtering and grouping:
Filter health check traces
Filter health check traces
Exclude health check endpoints from tracing to reduce noise (done by default in ServiceDefaults).
Troubleshooting
No telemetry in dashboard
No telemetry in dashboard
Verify:
- Service calls
builder.AddServiceDefaults() OTEL_EXPORTER_OTLP_ENDPOINTis set- Dashboard OTLP endpoint is accessible
Custom metrics not appearing
Custom metrics not appearing
Ensure the meter is registered:
Traces not correlated across services
Traces not correlated across services
Too much log data
Too much log data
Adjust log levels in appsettings.json:
Next Steps
Dashboard
Explore telemetry in the Aspire Dashboard
Service Discovery
Configure service-to-service communication
Application Model
Understand resources and the app model
Deployment
Deploy applications with telemetry to production