OpenTelemetry.Extensions.Hosting 1.11.2

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
CheddarAuthService
Package to access SIERA+ related authentication services and models
582
CheeseDataService
Package to access SIERA+ related data services and models
538
DataEstimationService
Package to access SIERA+ data estimation services and models
532
CheddarAuthService
Package to access SIERA+ related authentication services and models
92
CheddarAuthService
Package to access SIERA related authentication services and models
92
CheeseDataService
Package to access SIERA related data services and models
85
DataEstimationService
Package to access SIERA data estimation services and models
79
CheeseDataService
Package to access SIERA related data services and models
58
DataEstimationService
Package to access SIERA data estimation services and models
54
CheddarAuthService
Package to access SIERA related authentication services and models
49
Aspire.Hosting
Core abstractions for the Aspire application model.
41
CheeseDataService
Package to access SIERA related data services and models
35
WireMock.Net.OpenTelemetry
OpenTelemetry exporter support for WireMock.Net
31
WireMock.Net.OpenTelemetry
OpenTelemetry exporter support for WireMock.Net
30
Aspire.StackExchange.Redis
A generic Redis® client that integrates with Aspire, including health checks, logging, and telemetry.
30
WireMock.Net.OpenTelemetry
OpenTelemetry exporter support for WireMock.Net
29
WireMock.Net.OpenTelemetry
OpenTelemetry exporter support for WireMock.Net
28
Aspire.StackExchange.Redis
A generic Redis® client that integrates with Aspire, including health checks, logging, and telemetry.
28
CheddarAuthService
Package to access SIERA related authentication services and models
27

For highlights and announcements see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.11.2/RELEASENOTES.md.

    For detailed changes see: https://github.com/open-telemetry/opentelemetry-dotnet/blob/core-1.11.2/src/OpenTelemetry.Extensions.Hosting/CHANGELOG.md.

Version Downloads Last updated
1.22.0-rc.1 0 12/02/2026
1.21.0 0 11/11/2026
1.21.0-rc.2 1 11/21/2026
1.21.0-rc.1 2 11/20/2026
1.20.1 4 10/24/2026
1.20.1-rc.1 5 10/24/2026
1.20.0 9 10/17/2026
1.20.0-rc.1 7 10/17/2026
1.19.1 13 09/23/2026
1.19.1-rc.1 11 10/09/2026
1.19.0 9 09/26/2026
1.19.0-rc.1 7 09/25/2026
1.18.0 16 08/31/2026
1.18.0-rc.1 6 09/01/2026
1.17.0 17 07/16/2026
1.17.0-rc.1 11 07/17/2026
1.16.0 0 06/10/2026
1.16.0-rc.1 10 06/23/2026
1.15.3 0 04/21/2026
1.15.2 14 07/10/2026
1.15.1 17 06/20/2026
1.15.0 16 08/11/2026
1.14.0 16 07/04/2026
1.14.0-rc.1 13 06/23/2026
1.13.1 14 06/20/2026
1.13.0 13 06/20/2026
1.12.0 13 08/05/2026
1.11.2 10 07/09/2026
1.11.1 12 07/09/2026
1.11.0 18 08/05/2026
1.11.0-rc.1 16 06/23/2026
1.10.0 14 01/13/2025
1.10.0-rc.1 12 06/20/2026
1.10.0-beta.1 15 06/22/2026
1.9.0 13 11/18/2024
1.9.0-rc.1 18 06/20/2026
1.9.0-alpha.1 8 06/22/2026
1.8.1 11 06/20/2024
1.8.0 8 04/23/2024
1.8.0-rc.1 16 06/22/2026
1.8.0-beta.1 13 06/23/2026
1.7.0 35 02/05/2024
1.7.0-rc.1 14 02/05/2025
1.7.0-alpha.1 21 01/10/2024
1.6.0 20 01/05/2024
1.6.0-rc.1 14 01/10/2024
1.6.0-alpha.1 13 01/10/2024
1.5.1 13 01/10/2024
1.5.0 16 12/14/2023
1.5.0-rc.1 451 01/08/2024
1.5.0-alpha.2 10 01/10/2024
1.5.0-alpha.1 13 01/10/2024
1.4.0 16 01/10/2024
1.4.0-rc.4 18 01/10/2024
1.4.0-rc.3 18 01/10/2024
1.4.0-rc.2 16 01/10/2024
1.4.0-rc.1 10 01/10/2024
1.0.0-rc9.9 11 01/10/2024
1.0.0-rc9.8 12 01/10/2024
1.0.0-rc9.7 12 01/10/2024
1.0.0-rc9.6 13 01/10/2024
1.0.0-rc9.5 21 01/10/2024
1.0.0-rc9.4 21 01/10/2024
1.0.0-rc9.3 18 01/10/2024
1.0.0-rc9.2 11 01/10/2024
1.0.0-rc9.1 13 01/10/2024
1.0.0-rc9 22 01/10/2024
1.0.0-rc8 18 01/10/2024
1.0.0-rc7 13 01/09/2024
1.0.0-rc6 17 01/09/2024
1.0.0-rc5 16 01/10/2024
1.0.0-rc4 15 01/09/2024
1.0.0-rc3 13 01/10/2024
1.0.0-rc2 23 01/10/2024
1.0.0-rc10 11 01/09/2024
1.0.0-rc1.1 13 12/26/2023
0.8.0-beta.1 18 01/10/2024
0.7.0-beta.1 11 01/10/2024
0.6.0-beta.1 25 01/09/2024
0.5.0-beta.2 13 01/10/2024
0.4.0-beta.2 18 01/10/2024
0.3.0-beta.1 12 01/10/2024
0.2.0-alpha.275 14 01/10/2024