Setting Up OpenTelemetry Tracing with Dependency Injection in a .NET Console App
Автор: vlogommentary
Загружено: 2026-01-04
Просмотров: 1
Описание:
Learn how to properly configure OpenTelemetry tracing in a .NET console application using dependency injection and the HostApplicationBuilder for reliable telemetry data.
---
This video is based on the question https://stackoverflow.com/q/79425248/ asked by the user 'Matthew MacFarland' ( https://stackoverflow.com/u/680773/ ) and on the answer https://stackoverflow.com/a/79425491/ provided by the user 'MartinDotNet' ( https://stackoverflow.com/u/675149/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: What is the correct method to setup OpenTelemetry tracing in a .NET console app using dependency injection?
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
OpenTelemetry (OTEL) provides powerful telemetry collection capabilities including distributed tracing. However, setting up OpenTelemetry tracing correctly in a .NET console app with dependency injection (DI) can be tricky.
This guide explains the best practices to configure OpenTelemetry tracing using DI in a .NET console application, avoiding common pitfalls that prevent traces from being collected and exported.
Common Issue
Many developers struggle to get traces to emit when:
Creating ActivitySource instances non-statically.
Registering OpenTelemetry tracing services directly on IServiceCollection without using a proper host builder.
Not disposing of the TracerProvider, resulting in telemetry not being flushed.
If you see no incoming traces in your OTEL collector or exporters, these are common causes.
Best Practices for OpenTelemetry Tracing Setup
1. Use a Static ActivitySource
Create a static readonly ActivitySource per instrumentation scope—usually per project or library. This aligns with OpenTelemetry semantics:
[[See Video to Reveal this Text or Code Snippet]]
This lets you inject or access the same source throughout your app without recreating it.
2. Use HostApplicationBuilder Instead of Raw DI
Instead of configuring IServiceCollection manually, leverage the .NET HostApplicationBuilder to bootstrap your console app:
[[See Video to Reveal this Text or Code Snippet]]
Using the host builder ensures that OpenTelemetry background services start correctly, including the tracer provider lifecycle.
3. Dispose the TracerProvider Gracefully
To ensure all telemetry is flushed, dispose the TracerProvider or stop the host gracefully:
[[See Video to Reveal this Text or Code Snippet]]
This avoids losing traces that were still buffered.
4. Access ActivitySource via Injection or Static Access
If you prefer using DI, inject the static ActivitySource wrapper or create a singleton service exposing the static source:
[[See Video to Reveal this Text or Code Snippet]]
Example: Minimal Working DI Setup
[[See Video to Reveal this Text or Code Snippet]]
Summary
To effectively enable OpenTelemetry tracing with DI in a .NET console app:
Use a static ActivitySource shared across your app.
Configure OpenTelemetry tracing via HostApplicationBuilder to ensure correct lifecycle and background service activation.
Dispose the tracer provider either by stopping the host gracefully or explicitly calling Dispose for flushing.
Inject shared tracing resources or access static sources to create and manage activities.
Following these steps will resolve common issues where traces do not appear in your telemetry backend.
Happy tracing!
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: