Microsoft.Extensions.Telemetry 9.6.0

Prefix Reserved
The specified version 9.4.0-preview.1.25168.1 was not found. You have been taken to version 9.6.0.
dotnet add package Microsoft.Extensions.Telemetry --version 9.6.0
                    
NuGet\Install-Package Microsoft.Extensions.Telemetry -Version 9.6.0
                    
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="Microsoft.Extensions.Telemetry" Version="9.6.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Microsoft.Extensions.Telemetry" Version="9.6.0" />
                    
Directory.Packages.props
<PackageReference Include="Microsoft.Extensions.Telemetry" />
                    
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 Microsoft.Extensions.Telemetry --version 9.6.0
                    
#r "nuget: Microsoft.Extensions.Telemetry, 9.6.0"
                    
#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=Microsoft.Extensions.Telemetry&version=9.6.0
                    
Install Microsoft.Extensions.Telemetry as a Cake Addin
#tool nuget:?package=Microsoft.Extensions.Telemetry&version=9.6.0
                    
Install Microsoft.Extensions.Telemetry as a Cake Tool

Microsoft.Extensions.Telemetry

This library provides advanced logging and telemetry enrichment capabilities for .NET applications. It allows for detailed and configurable enrichment of log entries, along with enhanced latency monitoring and logging features. It is built for applications needing sophisticated telemetry and logging insights.

Install the package

From the command-line:

dotnet add package Microsoft.Extensions.Telemetry

Or directly in the C# project file:

<ItemGroup>
  <PackageReference Include="Microsoft.Extensions.Telemetry" Version="[CURRENTVERSION]" />
</ItemGroup>

Usage

Log Sampling

The library provides two types of log sampling mechanisms: Random Probabilistic Sampling and Trace-based Sampling.

Random Probabilistic Sampling

Provides configurable probability-based sampling with flexible rules:

// Simple configuration with probability
builder.Logging.AddRandomProbabilisticSampler(0.1); // Sample 10% of all logs, meaning - 90% of logs will be dropped
builder.Logging.AddRandomProbabilisticSampler(0.1, LogLevel.Warning); // Sample 10% of Warning and lower level logs

// Configuration using options
builder.Logging.AddRandomProbabilisticSampler(options =>
{
    options.Rules.Add(new RandomProbabilisticSamplerFilterRule(0.1, logLevel: LogLevel.Information)); // Sample 10% of Information and lower level logs
    options.Rules.Add(new RandomProbabilisticSamplerFilterRule(1.0, logLevel: LogLevel.Error)); // Sample all Error logs
});

// Configuration using IConfiguration
builder.Logging.AddRandomProbabilisticSampler(configuration.GetSection("Logging:Sampling"));

The Random Probabilistic Sampler supports the IOptionsMonitor<T> pattern, allowing for dynamic configuration updates. This means you can change the sampling rules at runtime without needing to restart your application.

Trace-Based Sampling

Matches logging sampling decisions with the underlying Distributed Tracing sampling decisions:

// Add trace-based sampler
builder.Logging.AddTraceBasedSampler();

This comes in handy when you already use OpenTelemetry .NET Tracing and would like to see sampling decisions being consistent across both logs and their underlying Activity.

Log Buffering

Provides a buffering mechanism for logs, allowing you to store logs in temporary circular buffers in memory. If the buffer is full, the oldest logs will be dropped. If you want to emit the buffered logs, you can call Flush() on the buffer. That way, if you don't flush buffers, all buffered logs will eventually be dropped and that makes sense - if you don't flush buffers, chances are those logs are not important. At the same time, you can trigger a flush on the buffer when certain conditions are met, such as when an exception occurs.

This library works with all logger providers, even if they do not implement the Microsoft.Extensions.Logging.Abstractions.IBufferedLogger interface. In that case, the library will be calling ILogger.Log() method directly on every single buffered log record when flushing the buffer.

Global Buffering

Provides application-wide log buffering with configurable rules:

// Simple configuration with log level
builder.Logging.AddGlobalBuffer(LogLevel.Warning); // Buffer Warning and lower level logs

// Configuration using options
builder.Logging.AddGlobalBuffer(options =>
{
    options.Rules.Add(new LogBufferingFilterRule(logLevel: LogLevel.Information)); // Buffer Information and lower level logs
    options.Rules.Add(new LogBufferingFilterRule(categoryName: "Microsoft.*")); // Buffer logs from Microsoft namespaces
});

// Configuration using IConfiguration
builder.Logging.AddGlobalBuffer(configuration.GetSection("Logging:Buffering"));

Then, to flush the global buffer when a bad thing happens, call the Flush() method on the injected GlobalLogBuffer instance:

public class MyService
{
    private readonly GlobalLogBuffer _globalLogBuffer;

    public MyService(GlobalLogBuffer globalLogBuffer)
    {
        _globalLogBuffer = globalLogBuffer;
    }

    public void DoSomething()
    {
        try
        {    
            // ...
        }
        catch (Exception ex)
        {
            // Flush the global buffer when an exception occurs
            _globalLogBuffer.Flush();
        }
    }
}

The Global Log Buffer supports the IOptionsMonitor<T> pattern, allowing for dynamic configuration updates. This means you can change the buffering rules at runtime without needing to restart your application.

Limitations
  1. This library does not preserve the order of log records. However, original timestamps are preserved.
  2. The library does not support custom configuration per each logger provider. Same configuration is applied to all logger providers.
  3. Log scopes are not supported. This means that if you use ILogger.BeginScope() method, the buffered log records will not be associated with the scope.
  4. When buffering and then flushing buffers, not all information of the original log record is preserved. This is due to serializing/deserializing limitation, but can be revisited in future. Namely, this library uses Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord class when converting buffered log records to actual log records, but omits following properties:
  • Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.ActivitySpanId
  • Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.ActivityTraceId
  • Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.ManagedThreadId
  • Microsoft.Extensions.Logging.Abstractions.BufferedLogRecord.MessageTemplate

Service Log Enrichment

Enriches logs with application-specific information based on ApplicationMetadata information. The bellow calls will add the service log enricher to the service collection.

// Add service log enricher with default settings
builder.Services.AddServiceLogEnricher();

// Or configure with options
builder.Services.AddServiceLogEnricher(options =>
{
    options.ApplicationName = true;
    options.BuildVersion = true;
    options.DeploymentRing = true;
    options.EnvironmentName = true;
});

Latency Monitoring

Provides tools for latency data collection and export. The bellow example uses the built-in Console exporter, but custom exporters can be created by implementing the ILatencyDataExporter interface.

// Add latency console data exporter with configuration
builder.Services.AddConsoleLatencyDataExporter(options =>
{
    options.OutputCheckpoints = true;
    options.OutputMeasures = true;
    options.OutputTags = true;
});

In order for the latency data to be exported, a call to ILatencyDataExporter.ExportAsync() is required. This can either be called manually, or by using the Request Latency Middleware inside the Microsoft.AspNetCore.Diagnostics.Middleware package by adding:

// Add Latency Context
builder.Services.AddLatencyContext();

// Add Checkpoints, Measures, Tags
builder.Services.RegisterCheckpointNames("databaseQuery", "externalApiCall");
builder.Services.RegisterMeasureNames("responseTime", "processingTime");
builder.Services.RegisterTagNames("userId", "transactionId");

// Add Console Latency exporter.
builder.Services.AddConsoleLatencyDataExporter();
// Optionally add custom exporters.
builder.Services.AddSingleton<ILatencyDataExporter, MyCustomExporter>();

// Add Request latency telemetry.
builder.Services.AddRequestLatencyTelemetry();

// ...

// Add Request Latency Middleware which will automatically call ExportAsync on all registered latency exporters.
app.UseRequestLatencyTelemetry();

Logging Enhancements

Offers additional logging capabilities like stack trace capturing, exception message inclusion, and log redaction.

// Enable log enrichment.
builder.Logging.EnableEnrichment(options =>
{
    options.CaptureStackTraces = true;
    options.IncludeExceptionMessage = true;
    options.MaxStackTraceLength = 500;
    options.UseFileInfoForStackTraces = true;
});

builder.Services.AddServiceLogEnricher(); // <- This call is required in order for the enricher to be added into the service collection.

// Enable log redaction
builder.Logging.EnableRedaction(options =>
{
    options.ApplyDiscriminator = true;
});

builder.Services.AddRedaction(); // <- This call is required in order for the redactor provider to be added into the service collection.

Feedback & Contributing

We welcome feedback and contributions in our GitHub repo.

Product Compatible and additional computed target framework versions.
.NET 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.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (11)

Showing the top 5 NuGet packages that depend on Microsoft.Extensions.Telemetry:

Package Downloads
Microsoft.Extensions.Http.Diagnostics

Telemetry support for HTTP Client.

Microsoft.AspNetCore.Diagnostics.Middleware

ASP.NET Core middleware for collecting high-quality telemetry.

Neon.Service

Service library supporting application deployment as containers as well as in-process for testing

Tanka.GraphQL.Server

GraphQL Query, Mutation and Subscription library

Cratis.Applications

Package Description

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on Microsoft.Extensions.Telemetry:

Repository Stars
dadhi/DryIoc
DryIoc is fast, small, full-featured IoC Container for .NET
DataDog/dd-trace-dotnet
.NET Client Library for Datadog APM
foxminchan/BookWorm
The practical implementation of .NET Aspire using Microservices
Version Downloads Last updated
9.6.0 17,820 6/10/2025
9.5.0 549,273 5/13/2025
9.4.0 1,114,980 4/8/2025
9.3.0 957,354 3/11/2025
9.2.0 1,483,366 2/11/2025
9.1.0 892,342 1/14/2025
9.0.0 2,576,838 11/12/2024
9.0.0-preview.9.24507.7 107,171 10/8/2024
9.0.0-preview.8.24460.1 33,393 9/10/2024
9.0.0-preview.7.24412.10 6,214 8/14/2024
9.0.0-preview.6.24353.1 4,978 7/10/2024
9.0.0-preview.5.24311.7 8,801 6/11/2024
9.0.0-preview.4.24271.2 8,286 5/21/2024
9.0.0-preview.3.24209.3 9,592 4/11/2024
9.0.0-preview.2.24157.4 3,770 3/12/2024
9.0.0-preview.1.24108.1 2,222 2/13/2024
8.10.0 4,816,578 10/8/2024
8.9.1 1,565,505 9/6/2024
8.9.0 67,954 9/5/2024
8.8.0 903,168 8/13/2024
8.7.0 1,566,537 7/10/2024
8.6.0 988,675 6/11/2024
8.5.0 1,408,273 5/14/2024
8.4.0 2,351,279 4/9/2024
8.3.0 747,952 3/12/2024
8.2.0 1,287,752 2/13/2024
8.1.0 638,958 1/9/2024
8.0.0 1,606,133 11/14/2023
8.0.0-rc.2.23510.2 5,683 10/10/2023
8.0.0-rc.1.23453.1 1,866 9/12/2023
8.0.0-preview.7.23407.5 1,560 8/8/2023
8.0.0-preview.6.23360.2 826 7/12/2023
8.0.0-preview.5.23308.3 3,703 6/14/2023
8.0.0-preview.4.23273.7 4,253 5/23/2023