OpenTelemetry.Extensions.Hosting 1.19.0
OpenTelemetry.Extensions.Hosting
Installation
dotnet add package OpenTelemetry.Extensions.Hosting
Overview
The OpenTelemetry.Extensions.Hosting package provides extension methods for
automatically starting (and stopping) OpenTelemetry tracing (TracerProvider)
and metrics (MeterProvider) in ASP.NET
Core and
.NET Generic
hosts. These are completely optional extensions meant to simplify the
management of the OpenTelemetry SDK lifecycle.
Extension method reference
Targeting Microsoft.Extensions.DependencyInjection.IServiceCollection:
AddOpenTelemetry: Registers an IHostedService to automatically start tracing and/or metric services in the supplied IServiceCollection and then returns anOpenTelemetryBuilderclass.[!NOTE]
AddOpenTelemetryshould be called by application host code only. Library authors see: Registration extension method guidance for library authors.[!NOTE] Multiple calls to
AddOpenTelemetrywill NOT result in multiple providers. Only a singleTracerProviderand/orMeterProviderwill be created in the targetIServiceCollection. To establish multiple providers use theSdk.CreateTracerProviderBuilder()and/orSdk.CreateMeterProviderBuilder()methods. See TracerProvider configuration and Building a MeterProvider for more details.OpenTelemetryBuildermethods:ConfigureResource: Registers a callback action to configure theResourceBuilderfor tracing and metric providers.WithTracing: Enables tracing and optionally configures theTracerProvider.WithMetrics: Enables metrics and optionally configures theMeterProvider.
Targeting Microsoft.Extensions.Hosting.IHostApplicationBuilder:
AddOpenTelemetry: Does everything theIServiceCollectionmethod above does, using IHostApplicationBuilder.Services, and additionally:Seeds
service.namefromIHostEnvironment.ApplicationNameanddeployment.environment.namefromIHostEnvironment.EnvironmentNameas low-priority resource defaults. Both are superseded by theOTEL_SERVICE_NAME/OTEL_RESOURCE_ATTRIBUTESenvironment variables or by any explicitConfigureResourcecall.Makes the host's Configuration available to OpenTelemetry extensions during setup. It also registers that configuration as an
IConfigurationManagersingleton when the application has not already registered one.
It returns the same
OpenTelemetryBuilderclass, so the only change to an existing setup is the first line:var builder = Host.CreateApplicationBuilder(args); builder.AddOpenTelemetry() .WithTracing(tracing => tracing.AddSource("MyApp"));[!NOTE] A host registers
IConfigurationas a factory so that the container owns its disposal, which leaves the underlying configuration unreachable while the application is still being built. Registering it makes the application's configuration available to OpenTelemetry extensions that only receive anIServiceCollection, so they can contribute configuration sources during setup rather than replacing theIConfigurationregistration afterwards. AnIConfigurationManageralready registered by the application is left in place.
Usage
The following example shows how to register OpenTelemetry tracing & metrics in an ASP.NET Core host using the OpenTelemetry.Extensions.Hosting extensions.
using Microsoft.AspNetCore.Builder;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
var appBuilder = WebApplication.CreateBuilder(args);
appBuilder.AddOpenTelemetry()
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
var app = appBuilder.Build();
app.Run();
A fully functional example can be found here.
[!IMPORTANT] Applications that do not support hosted services, such as Blazor, should resolve the
ITelemetryHostInitializerservice from the service provider to manually initialize the OpenTelemetry SDK as part of application startup.For example:
app.Services.GetRequiredService<ITelemetryHostInitializer>().Initialize();
Resources
To dynamically add resources at startup from the dependency injection you can
provide an IResourceDetector.
To make use of it add it to the dependency injection and then you can use the
IServiceProvider to add it to OpenTelemetry:
public class MyResourceDetector : IResourceDetector
{
private readonly IWebHostEnvironment webHostEnvironment;
public MyResourceDetector(IWebHostEnvironment webHostEnvironment)
{
this.webHostEnvironment = webHostEnvironment;
}
public Resource Detect()
{
return ResourceBuilder.CreateEmpty()
.AddService(serviceName: this.webHostEnvironment.ApplicationName)
.AddAttributes(new Dictionary<string, object> { ["host.environment"] = this.webHostEnvironment.EnvironmentName })
.Build();
}
}
services.AddSingleton<MyResourceDetector>();
services.AddOpenTelemetry()
.ConfigureResource(builder =>
builder.AddDetector(sp => sp.GetRequiredService<MyResourceDetector>()))
.WithTracing(builder => builder.AddConsoleExporter())
.WithMetrics(builder => builder.AddConsoleExporter());
Migrating from pre-release versions of OpenTelemetry.Extensions.Hosting
Pre-release versions (all versions prior to 1.4.0) of
OpenTelemetry.Extensions.Hosting contained signal-specific methods for
configuring tracing and metrics:
AddOpenTelemetryTracing: Configure OpenTelemetry and register an IHostedService to automatically start tracing services in the supplied IServiceCollection.AddOpenTelemetryMetrics: Configure OpenTelemetry and register an IHostedService to automatically start metric services in the supplied IServiceCollection.
These methods were marked obsolete and later removed. You should migrate your
code to the new AddOpenTelemetry method documented above. Refer the
old
and
new
versions of the example application to assist you in your migration.
Hosted Service Ordering and Telemetry Capture
TBD
References
Showing the top 20 packages that depend on OpenTelemetry.Extensions.Hosting.
| Packages | Downloads |
|---|---|
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
2 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
5 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
7 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
8 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
11 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
12 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
13 |
|
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
|
15 |
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
|
5 |
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
|
9 |
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
|
10 |
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
|
12 |
|
Microsoft.ApplicationInsights.AspNetCore
Application Insights for ASP.NET Core web applications. See https://azure.microsoft.com/documentation/articles/app-insights-asp-net-five/ for more information. Privacy statement: https://go.microsoft.com/fwlink/?LinkId=512156
|
13 |
.NET 10.0
- OpenTelemetry (>= 1.19.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
.NET Framework 4.6.2
- OpenTelemetry (>= 1.19.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
.NET 8.0
- OpenTelemetry (>= 1.19.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
.NET 9.0
- OpenTelemetry (>= 1.19.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
.NET Standard 2.0
- OpenTelemetry (>= 1.19.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
| Version | Downloads | Last updated |
|---|---|---|
| 1.19.0 | 1 | 09/18/2026 |
| 1.19.0-rc.1 | 0 | 09/18/2026 |
| 1.18.0 | 3 | 09/04/2026 |
| 1.18.0-rc.1 | 1 | 09/05/2026 |
| 1.17.0 | 6 | 07/26/2026 |
| 1.17.0-rc.1 | 2 | 07/26/2026 |
| 1.16.0 | 3 | 06/15/2026 |
| 1.16.0-rc.1 | 2 | 06/15/2026 |
| 1.15.3 | 3 | 05/06/2026 |
| 1.15.2 | 4 | 04/11/2026 |
| 1.15.1 | 4 | 04/07/2026 |
| 1.15.0 | 10 | 01/23/2026 |
| 1.14.0 | 12 | 11/21/2025 |
| 1.14.0-rc.1 | 11 | 10/24/2025 |
| 1.13.1 | 12 | 10/14/2025 |
| 1.13.0 | 13 | 10/07/2025 |
| 1.12.0 | 17 | 05/15/2025 |
| 1.11.2 | 30 | 03/05/2025 |
| 1.11.1 | 18 | 01/31/2025 |
| 1.11.0 | 18 | 01/18/2025 |
| 1.11.0-rc.1 | 18 | 01/19/2025 |
| 1.10.0 | 19 | 01/19/2025 |
| 1.10.0-rc.1 | 20 | 01/19/2025 |
| 1.10.0-beta.1 | 20 | 01/19/2025 |
| 1.9.0 | 20 | 01/19/2025 |
| 1.9.0-rc.1 | 21 | 01/19/2025 |
| 1.9.0-alpha.1 | 21 | 01/19/2025 |
| 1.8.1 | 22 | 01/19/2025 |
| 1.8.0 | 22 | 01/19/2025 |
| 1.8.0-rc.1 | 26 | 01/31/2025 |
| 1.8.0-beta.1 | 20 | 01/19/2025 |
| 1.7.0 | 21 | 03/13/2024 |
| 1.7.0-rc.1 | 22 | 03/19/2024 |
| 1.7.0-alpha.1 | 24 | 01/13/2024 |
| 1.6.0 | 20 | 03/14/2024 |
| 1.6.0-rc.1 | 22 | 03/18/2024 |
| 1.6.0-alpha.1 | 24 | 03/18/2024 |
| 1.5.1 | 21 | 03/17/2024 |
| 1.5.0 | 20 | 03/14/2024 |
| 1.5.0-rc.1 | 21 | 03/18/2024 |
| 1.5.0-alpha.2 | 22 | 03/19/2024 |
| 1.5.0-alpha.1 | 19 | 03/19/2024 |
| 1.4.0 | 22 | 03/19/2024 |
| 1.4.0-rc.4 | 20 | 03/18/2024 |
| 1.4.0-rc.3 | 22 | 03/20/2024 |
| 1.4.0-rc.2 | 23 | 03/20/2024 |
| 1.4.0-rc.1 | 20 | 03/20/2024 |
| 1.0.0-rc9.9 | 23 | 03/19/2024 |
| 1.0.0-rc9.8 | 23 | 03/18/2024 |
| 1.0.0-rc9.7 | 21 | 03/19/2024 |
| 1.0.0-rc9.6 | 25 | 03/18/2024 |
| 1.0.0-rc9.5 | 22 | 03/18/2024 |
| 1.0.0-rc9.4 | 23 | 03/20/2024 |
| 1.0.0-rc9.3 | 22 | 03/20/2024 |
| 1.0.0-rc9.2 | 22 | 03/20/2024 |
| 1.0.0-rc9.1 | 20 | 03/19/2024 |
| 1.0.0-rc9 | 24 | 03/18/2024 |
| 1.0.0-rc8 | 20 | 03/18/2024 |
| 1.0.0-rc7 | 20 | 03/19/2024 |
| 1.0.0-rc6 | 20 | 03/18/2024 |
| 1.0.0-rc5 | 22 | 03/18/2024 |
| 1.0.0-rc4 | 22 | 03/18/2024 |
| 1.0.0-rc3 | 23 | 03/19/2024 |
| 1.0.0-rc2 | 26 | 03/18/2024 |
| 1.0.0-rc10 | 23 | 03/19/2024 |
| 1.0.0-rc1.1 | 22 | 03/18/2024 |
| 0.8.0-beta.1 | 24 | 03/18/2024 |
| 0.7.0-beta.1 | 21 | 03/18/2024 |
| 0.6.0-beta.1 | 21 | 03/18/2024 |
| 0.5.0-beta.2 | 23 | 03/17/2024 |
| 0.4.0-beta.2 | 20 | 03/19/2024 |
| 0.3.0-beta.1 | 25 | 03/18/2024 |
| 0.2.0-alpha.275 | 20 | 03/18/2024 |