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-generator.md
+14-21Lines changed: 14 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,13 @@ ms.date: 04/11/2025
6
6
7
7
# Compile-time metric source generation
8
8
9
-
.NET's metering infrastructure is designed to deliver a highly-usable and high-performance metering solution
10
-
for modern .NET applications.
9
+
.NET's metering infrastructure is designed to deliver a highly usable and high-performance metering solution for modern .NET applications.
11
10
12
-
To use source generated metering, you first create a class that defines the names and dimensions of the metrics your code can produce.
13
-
Then you need to create the class with partial methods signatures.
11
+
To use source-generated metering, create a class that defines the names and dimensions of the metrics your code can produce. Then, create the class with `partial` method signatures.
14
12
15
-
Then, the .NET's code generator automatically generates the code, which exposes strongly-typed metering types and
16
-
methods that you can invoke to record metric values. The generated methods are implemented in a highly efficient
17
-
form, which reduces computation overhead as compared to traditional metering solutions.
13
+
The code generator automatically generates the code, which exposes strongly typed metering types and methods that you can invoke to record metric values. The generated methods are implemented in a highly efficient form, which reduces computation overhead as compared to traditional metering solutions.
18
14
19
-
## Install the package
15
+
## Get started
20
16
21
17
To get started, install the [📦 Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions) NuGet package:
For more information, see [dotnet add package](../tools/dotnet-package-add.md) or [Manage package dependencies in .NET applications](../tools/dependencies.md).
41
37
42
-
Generic attributes are only supported in C# 11 or later, so if you are using generic attributes then please enable C# 11 or later.
43
-
Please use non generic attributes if you are using C# 10 or earlier.
38
+
## Generic attributes
44
39
45
-
## Usage
40
+
Generic attributes require C# 11 or later. For C# 10 or earlier, use nongeneric attributes instead.
46
41
47
42
The following example shows a class that declares two metrics. The methods are marked with an attribute and are declared as `static` and `partial`.
48
43
The code generator runs at build time and provides an implementation of these methods, along with accompanying
@@ -100,9 +95,7 @@ internal class Latency
100
95
}
101
96
```
102
97
103
-
The dimensions specified in the attributes have been turned into arguments to the `Add` and `Record` methods.
104
-
You then use the generated methods to create instances of these types. With the instancescreated,
105
-
youcancall `Add` and `Record` toregistermetricvalues, likethis:
98
+
The dimensions specified in the attributes have been turned into arguments to the `Add` and `Record` methods. You then use the generated methods to create instances of these types. With the instancescreated, youcancall `Add` and `Record` toregistermetricvalues, asshowninthefollowingexample:
106
99
107
100
```csharp
108
101
internalclassMyClass
@@ -148,15 +141,15 @@ internal class MyClass
148
141
149
142
## Metric methods requirements
150
143
151
-
Metric methods have some constraints that you must follow:
144
+
Metric methods are constrained to the following:
152
145
153
-
- They must be static, partial, and public.
146
+
- They must be `public static partial`.
154
147
- The return type must be unique.
155
148
- Their names must not start with an underscore.
156
149
- Their parameter names must not start with an underscore.
157
-
- Their first parameter must be `Meter`.
158
-
- They can't be generic or accept any generic parameters.
150
+
- Their first parameter must be <xref:System.Diagnostics.Metrics.Meter> type.
151
+
Metric methods are constrained to the following:
159
152
160
-
## Supported metrics
153
+
## See also
161
154
162
-
Please refer to the .NET [`Types of instruments`](metrics-instrumentation.md#types-of-instruments) to learn about all the supported instruments and description on how to choose which instrument to use in different situations.
155
+
For more information on the supported metrics, see [Types of instruments](metrics-instrumentation.md#types-of-instruments) to learn how to choose which instrument to use in different situations.
Copy file name to clipboardExpand all lines: docs/core/diagnostics/metrics-strongly-typed.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ Modern .NET applications can capture metrics using the <xref:System.Diagnostics.
11
11
> [!NOTE]
12
12
> In the context of metrics, a tag is sometimes also called a "dimension." This article uses "tag" for clarity and consistency with .NET metrics terminology.
13
13
14
-
## Install the package
14
+
## Get started
15
15
16
16
To get started, install the [📦 Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions) NuGet package:
For more information, see [dotnet add package](../tools/dotnet-package-add.md) or [Manage package dependencies in .NET applications](../tools/dependencies.md).
36
+
35
37
## Tag name defaults and customization
36
38
37
39
By default, the source generator derives metric tag names from the field and property names of your tag class. In other words, each public field or property in the strongly-typed tag object becomes a tag name by default. You can override this by using the <xref:Microsoft.Extensions.Diagnostics.Metrics.TagNameAttribute> on a field or property to specify a custom tag name. In the examples below, you’ll see both approaches in action.
0 commit comments