You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/core/diagnostics/metrics-collection.md
+33-1Lines changed: 33 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
title: Collect metrics - .NET
3
3
description: Tutorial to collect metrics in .NET applications
4
4
ms.topic: tutorial
5
-
ms.date: 10/27/2021
5
+
ms.date: 08/27/2026
6
6
---
7
7
8
8
# Collect metrics
@@ -21,6 +21,38 @@ For more information on custom metric instrumentation and options, see [Compare
21
21
22
22
-[.NET 6.0 SDK](https://dotnet.microsoft.com/download/dotnet) or a later
23
23
24
+
## Configure metrics with IConfiguration
25
+
26
+
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:
Import the `Microsoft.Extensions.DependencyInjection` and `Microsoft.Extensions.Diagnostics.Metrics` namespaces to use these extension methods.
34
+
35
+
> [!IMPORTANT]
36
+
> 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.
37
+
38
+
Use the following sections to choose which meters the rules apply to:
39
+
40
+
| Section | Applies to |
41
+
| --- | --- |
42
+
|`EnabledMetrics`| Both global and local meters. |
43
+
|`EnabledGlobalMetrics`| Meters created directly with <xref:System.Diagnostics.Metrics.Meter>. |
44
+
|`EnabledLocalMetrics`| Meters created by the service container's <xref:System.Diagnostics.Metrics.IMeterFactory>. |
45
+
46
+
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.
47
+
48
+
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.
49
+
50
+
The following `appsettings.json` example enables the `Contoso.Web` meter by default but disables its noisy `request-duration` instrument:
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.
55
+
24
56
## Create an example app
25
57
26
58
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).
0 commit comments