OpenTelemetry.Extensions.Hosting 1.11.2

Prefix Reserved
dotnet add package OpenTelemetry.Extensions.Hosting --version 1.11.2
                    
NuGet\Install-Package OpenTelemetry.Extensions.Hosting -Version 1.11.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.11.2" />
                    
Directory.Packages.props
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenTelemetry.Extensions.Hosting --version 1.11.2
                    
#r "nuget: OpenTelemetry.Extensions.Hosting, 1.11.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=OpenTelemetry.Extensions.Hosting&version=1.11.2
                    
Install OpenTelemetry.Extensions.Hosting as a Cake Addin
#tool nuget:?package=OpenTelemetry.Extensions.Hosting&version=1.11.2
                    
Install OpenTelemetry.Extensions.Hosting as a Cake Tool

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (366)

Showing the top 5 NuGet packages that depend on OpenTelemetry.Extensions.Hosting:

Package Downloads
Azure.Monitor.OpenTelemetry.Exporter

An OpenTelemetry .NET exporter that exports to Azure Monitor

Azure.Monitor.OpenTelemetry.AspNetCore

An OpenTelemetry .NET distro that exports to Azure Monitor

Sitko.Core.App

Sitko.Core is a set of libraries to help build .NET Core applications fast

Steeltoe.Management.OpenTelemetryBase

Steeltoe Management OpenTelemetry

IEvangelist.Azure.CosmosRepository

This client library enables client applications to connect to Azure Cosmos via a repository pattern around the official Azure Cosmos .NET SDK. Azure Cosmos is a globally distributed, multi-model database service. For more information, refer to http://azure.microsoft.com/services/cosmos-db/.

GitHub repositories (153)

Showing the top 20 popular GitHub repositories that depend on OpenTelemetry.Extensions.Hosting:

Repository Stars
microsoft/semantic-kernel
Integrate cutting-edge LLM technology quickly and easily into your apps
jasontaylordev/CleanArchitecture
Clean Architecture Solution Template for ASP.NET Core
ardalis/CleanArchitecture
Clean Architecture Solution Template: A proven Clean Architecture Template for ASP.NET Core 9
dotnet/runtime
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
dotnet/AspNetCore.Docs
Documentation for ASP.NET Core
JustArchiNET/ArchiSteamFarm
C# application with primary purpose of farming Steam cards from multiple accounts simultaneously.
dotnet/orleans
Cloud Native application framework for .NET
dodyg/practical-aspnetcore
Practical samples of ASP.NET Core 10 Preview 3, 9, 8.0, 7.0, 6.0, 5.0, 3.1, 2.2, and 2.1,projects you can use. Readme contains explanations on all projects.
dotnet/eShop
A reference .NET application implementing an eCommerce site
MassTransit/MassTransit
Distributed Application Framework for .NET
quartznet/quartznet
Quartz Enterprise Scheduler .NET
graphql-dotnet/graphql-dotnet
GraphQL for .NET
Azure/azure-sdk-for-net
This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
fullstackhero/dotnet-starter-kit
Production Grade Cloud-Ready .NET 9 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
kurrent-io/EventStore
EventStoreDB, the event-native database. Designed for Event Sourcing, Event-Driven, and Microservices architectures
dotnet/aspire
Tools, templates, and packages to accelerate building observable, production-ready apps
grpc/grpc-dotnet
gRPC for .NET
microsoft/fluentui-blazor
Microsoft Fluent UI Blazor components library. For use with ASP.NET Core Blazor applications
ravendb/ravendb
ACID Document Database
oskardudycz/EventSourcing.NetCore
Examples and Tutorials of Event Sourcing in .NET
Version Downloads Last updated
1.11.2 1,829,492 3/4/2025
1.11.1 2,548,300 1/23/2025
1.11.0 577,978 1/16/2025
1.11.0-rc.1 24,353 12/12/2024
1.10.0 4,117,682 11/12/2024
1.10.0-rc.1 10,440 11/1/2024
1.10.0-beta.1 28,866 9/30/2024
1.9.0 25,162,320 6/14/2024
1.9.0-rc.1 216,304 6/7/2024
1.9.0-alpha.1 51,656 5/20/2024
1.8.1 12,444,089 4/18/2024
1.8.0 3,110,508 4/3/2024
1.8.0-rc.1 21,928 3/27/2024
1.8.0-beta.1 58,932 3/14/2024
1.7.0 10,374,215 12/9/2023
1.7.0-rc.1 115,270 11/30/2023
1.7.0-alpha.1 491,814 10/17/2023
1.6.0 10,050,705 9/6/2023
1.6.0-rc.1 388,766 8/21/2023
1.6.0-alpha.1 313,919 7/12/2023
1.5.1 6,381,689 6/26/2023
1.5.0 1,546,571 6/6/2023 1.5.0 is deprecated.
1.5.0-rc.1 180,018 5/26/2023
1.5.0-alpha.2 1,028,667 4/1/2023
1.5.0-alpha.1 603,131 3/8/2023
1.4.0 8,991,745 2/24/2023
1.4.0-rc.4 724,927 2/11/2023
1.4.0-rc.3 592,839 2/2/2023
1.4.0-rc.2 746,334 1/9/2023
1.4.0-rc.1 1,174,707 12/12/2022
1.0.0-rc9.9 1,496,256 11/7/2022
1.0.0-rc9.8 514,658 10/17/2022
1.0.0-rc9.7 360,678 9/30/2022
1.0.0-rc9.6 1,306,425 8/18/2022
1.0.0-rc9.5 1,259,287 8/3/2022
1.0.0-rc9.4 6,638,359 6/3/2022
1.0.0-rc9.3 2,231,355 4/20/2022
1.0.0-rc9.2 2,051,671 4/13/2022
1.0.0-rc9.1 679,024 3/30/2022
1.0.0-rc9 3,046,442 2/3/2022
1.0.0-rc8 5,875,010 10/8/2021
1.0.0-rc7 2,034,071 7/13/2021
1.0.0-rc6 311,927 6/26/2021
1.0.0-rc5 411,538 6/9/2021
1.0.0-rc4 330,886 4/23/2021
1.0.0-rc3 499,442 3/19/2021
1.0.0-rc2 802,775 1/30/2021
1.0.0-rc10 122,774 3/5/2022
1.0.0-rc1.1 526,309 11/18/2020
0.8.0-beta.1 97,389 11/5/2020
0.7.0-beta.1 16,073 10/16/2020
0.6.0-beta.1 136,578 9/16/2020
0.5.0-beta.2 8,387 8/28/2020
0.4.0-beta.2 115,379 7/25/2020
0.3.0-beta.1 1,468 7/23/2020
0.2.0-alpha.275 186,301 5/19/2020