Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion docs/core/diagnostics/metrics-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Collect metrics - .NET
description: Tutorial to collect metrics in .NET applications
ms.topic: tutorial
ms.date: 10/27/2021
ms.date: 08/27/2026
---

# Collect metrics
Expand All @@ -21,6 +21,38 @@ For more information on custom metric instrumentation and options, see [Compare

- [.NET 6.0 SDK](https://dotnet.microsoft.com/download/dotnet) or a later

## Configure metrics with IConfiguration

Starting with .NET 8, Generic Host applications that use the default builder settings automatically call <xref:Microsoft.Extensions.DependencyInjection.MetricsServiceExtensions.AddMetrics*> and load the `Metrics` section from <xref:Microsoft.Extensions.Configuration.IConfiguration>. In a non-hosted dependency injection application, call `AddMetrics` and <xref:Microsoft.Extensions.Diagnostics.Metrics.MetricsBuilderConfigurationExtensions.AddConfiguration(Microsoft.Extensions.Diagnostics.Metrics.IMetricsBuilder,Microsoft.Extensions.Configuration.IConfiguration)> explicitly to bind the same section:

```csharp
services.AddMetrics(metrics =>
metrics.AddConfiguration(configuration.GetSection("Metrics")));
```

Import the `Microsoft.Extensions.DependencyInjection` and `Microsoft.Extensions.Diagnostics.Metrics` namespaces to use these extension methods.

> [!IMPORTANT]
> The `Metrics` configuration only selects which instruments registered <xref:Microsoft.Extensions.Diagnostics.Metrics.IMetricsListener> implementations receive. It doesn't register listeners or activate their subscriptions, create metrics, aggregate measurements, export data, or automatically configure OpenTelemetry, Prometheus, `dotnet-counters`, or any other collector.
Comment thread
rosebyte marked this conversation as resolved.

Use the following sections to choose which meters the rules apply to:

| Section | Applies to |
| --- | --- |
| `EnabledMetrics` | Both global and local meters. |
| `EnabledGlobalMetrics` | Meters created directly with <xref:System.Diagnostics.Metrics.Meter>. |
| `EnabledLocalMetrics` | Meters created by the service container's <xref:System.Diagnostics.Metrics.IMeterFactory>. |

An `EnabledMetrics`, `EnabledGlobalMetrics`, or `EnabledLocalMetrics` section directly under `Metrics` applies to every registered listener. To target one listener, nest the section under the listener's <xref:Microsoft.Extensions.Diagnostics.Metrics.IMetricsListener.Name> value, such as `Metrics:MyListener:EnabledMetrics`. Listener and instrument names require exact matches without regard to case.

Within any of these sections, a Boolean meter entry enables or disables every instrument whose meter name matches. To set instrument-level rules, use an object with instrument names as keys. The special `Default` entry supplies a fallback. A Boolean `Default` entry at the meter level applies to otherwise unmatched meters. Within a meter object, `Default` applies to otherwise unmatched instruments. A listener doesn't receive an instrument if no rule matches it.

The following `appsettings.json` example enables the `Contoso.Web` meter by default but disables its noisy `request-duration` instrument:

:::code language="json" source="snippets/Metrics/appsettings.json":::

Meter names use case-insensitive prefix matching. A meter name can contain at most one `*` wildcard to match a prefix and suffix. More-specific rules take precedence in this order: listener-specific rules, longer meter-name patterns, instrument-specific rules, and scope-specific rules. If equally specific rules conflict, the last configured rule wins.

## Create an example app

Before metrics can be collected, measurements must be produced. This tutorial creates an app that has basic metric instrumentation. The .NET runtime also has [various metrics built-in](built-in-metrics.md). For more information about creating new metrics using the <xref:System.Diagnostics.Metrics.Meter?displayProperty=nameWithType> API, see [the instrumentation tutorial](metrics-instrumentation.md).
Expand Down
10 changes: 10 additions & 0 deletions docs/core/diagnostics/snippets/Metrics/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Metrics": {
"EnabledMetrics": {
"Contoso.Web": {
"Default": true,
"request-duration": false
}
}
}
}
Loading