OpenTelemetry.Extensions.Hosting 1.15.0

OpenTelemetry.Extensions.Hosting

NuGet NuGet

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 an OpenTelemetryBuilder class.

    [!NOTE] AddOpenTelemetry should be called by application host code only. Library authors see: Registration extension method guidance for library authors.

    [!NOTE] Multiple calls to AddOpenTelemetry will NOT result in multiple providers. Only a single TracerProvider and/or MeterProvider will be created in the target IServiceCollection. To establish multiple providers use the Sdk.CreateTracerProviderBuilder() and/or Sdk.CreateMeterProviderBuilder() methods. See TracerProvider configuration and Building a MeterProvider for more details.

    OpenTelemetryBuilder methods:

    • ConfigureResource: Registers a callback action to configure the ResourceBuilder for tracing and metric providers.

    • WithTracing: Enables tracing and optionally configures the TracerProvider.

    • WithMetrics: Enables metrics and optionally configures the MeterProvider.

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 Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

var appBuilder = WebApplication.CreateBuilder(args);

appBuilder.Services.AddOpenTelemetry()
    .ConfigureResource(builder => builder.AddService(serviceName: "MyService"))
    .WithTracing(builder => builder.AddConsoleExporter())
    .WithMetrics(builder => builder.AddConsoleExporter());

var app = appBuilder.Build();

app.Run();

A fully functional example can be found here.

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:

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
13
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
17
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
20
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
21
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
23
Azure.Monitor.OpenTelemetry.Exporter
An OpenTelemetry .NET exporter that exports to Azure Monitor
40
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
11
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
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
37

For highlights and announcements see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.15.0/RELEASENOTES.md. For detailed changes see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.15.0/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md.

Version Downloads Last updated
1.15.0 16 01/21/2026
1.14.0 23 12/07/2025
1.14.0-rc.1 21 12/07/2025
1.13.1 20 12/07/2025
1.13.0 23 12/07/2025
1.12.0 19 12/07/2025
1.11.2 18 12/07/2025
1.11.1 21 12/07/2025
1.11.0 20 12/07/2025
1.11.0-rc.1 22 12/07/2025
1.10.0 18 12/07/2025
1.10.0-rc.1 19 12/07/2025
1.10.0-beta.1 22 12/07/2025
1.9.0 20 12/07/2025
1.9.0-rc.1 20 12/07/2025
1.9.0-alpha.1 20 12/07/2025
1.8.1 23 12/07/2025
1.8.0 20 12/07/2025
1.8.0-rc.1 20 12/07/2025
1.8.0-beta.1 18 12/07/2025
1.7.0 21 12/07/2025
1.7.0-rc.1 24 12/07/2025
1.7.0-alpha.1 19 12/07/2025
1.6.0 23 12/07/2025
1.6.0-rc.1 20 12/07/2025
1.6.0-alpha.1 20 12/07/2025
1.5.1 20 12/07/2025
1.5.0 21 12/07/2025
1.5.0-rc.1 22 12/07/2025
1.5.0-alpha.2 24 12/07/2025
1.5.0-alpha.1 20 12/07/2025
1.4.0 21 12/07/2025
1.4.0-rc.4 23 12/07/2025
1.4.0-rc.3 19 12/07/2025
1.4.0-rc.2 22 12/07/2025
1.4.0-rc.1 24 12/07/2025
1.0.0-rc9.9 19 12/07/2025
1.0.0-rc9.8 21 12/07/2025
1.0.0-rc9.7 24 12/07/2025
1.0.0-rc9.6 21 12/07/2025
1.0.0-rc9.5 21 12/07/2025
1.0.0-rc9.4 23 12/07/2025
1.0.0-rc9.3 22 12/07/2025
1.0.0-rc9.2 22 12/07/2025
1.0.0-rc9.1 22 12/07/2025
1.0.0-rc9 22 12/07/2025
1.0.0-rc8 23 12/07/2025
1.0.0-rc7 20 12/07/2025
1.0.0-rc6 18 12/07/2025
1.0.0-rc5 24 12/07/2025
1.0.0-rc4 24 12/07/2025
1.0.0-rc3 23 12/07/2025
1.0.0-rc2 20 12/07/2025
1.0.0-rc10 20 12/07/2025
1.0.0-rc1.1 23 12/07/2025
0.8.0-beta.1 18 12/07/2025
0.7.0-beta.1 21 12/07/2025
0.6.0-beta.1 21 12/07/2025
0.5.0-beta.2 21 12/07/2025
0.4.0-beta.2 18 12/07/2025
0.3.0-beta.1 18 12/07/2025
0.2.0-alpha.275 21 12/07/2025