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
description: Learn how to use the custom log enricher in .NET.
4
+
ms.date: 10/13/2025
5
+
---
6
+
7
+
# Custom log enricher
8
+
9
+
You can easily create a custom enricher by creating a class that implements the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher> interface.
10
+
After the class is created, you register it with <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher)>.
11
+
Once registered, the logging infrastructure automatically calls the `Enrich()` method exactly once on every registered enricher for each log message produced.
12
+
13
+
## Install the package
14
+
15
+
To get started, install the [📦 Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions) NuGet package:
Your custom enricher only needs to implement a single <xref:Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher.Enrich(Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector)> method.
41
+
During enrichment, this method is called and given an <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector> instance. The enricher then calls one of the overloads of
42
+
the <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Add(System.String,System.Object)> method to record any properties it wants.
43
+
44
+
> [!NOTE]
45
+
> If your custom log enricher calls <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Add(System.String,System.Object)>,
46
+
> it is acceptable to send any type of argument to the `value` parameter as is, because it is parsed into the actual type and serialized internally
And you register it as shown in the following code using <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:
And you register it as shown in the following code <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher)>:
description: Learn about log enrichment in .NET and how to enhance your logs with contextual information.
4
+
ms.date: 10/13/2025
5
+
---
6
+
7
+
# Overview
8
+
9
+
Log enrichment is a powerful feature that automatically attaches contextual information to your application's logs. Instead of manually adding metadata to each log, enrichment provides a systematic way to inject relevant context automatically across your entire application.
10
+
11
+
## What is enrichment?
12
+
13
+
Enrichment augments telemetry objects with additional information that provides valuable context about the environment, application state, and execution context when the telemetry was generated. This contextual data helps with debugging, monitoring, performance analysis, and understanding application behavior in production environments.
14
+
15
+
## Why is enrichment important?
16
+
17
+
Enrichment plays a critical role in enhancing observability and diagnostics by adding standardized contextual information—such as process details, environment tags, or user identifiers—to telemetry data. This additional metadata transforms raw logs into structured, meaningful insights, making it easier to trace issues, correlate events, and improve application reliability. By enabling enrichment and configuring specific enrichers, teams can streamline troubleshooting, optimize performance monitoring, and ensure compliance with operational standards. Ultimately, enrichment is not just a technical add-on; it’s a foundational practice for building resilient, transparent systems that support informed decision-making and faster incident resolution.
18
+
19
+
## How enrichment works
20
+
21
+
The enrichment framework operates through a collection of enrichers that are registered with the dependency injection container. When telemetry is generated, all registered enrichers automatically contribute their contextual information to the telemetry payload. You just register the specific set of enrichers you want into an <xref:Microsoft.Extensions.DependencyInjection.IServiceCollection> instance. The enrichers run automatically without requiring changes to your application code. You simply configure which enrichers you want to use during application startup.
22
+
23
+
## Dimension names and tags
24
+
25
+
Enrichers add information to telemetry using standardized dimension names (also called tags or keys).
26
+
27
+
## Setting up enrichment
28
+
29
+
To use log enrichment in your application, you need to:
30
+
31
+
1.**Enable enrichment** for logging.
32
+
2.**Register specific enrichers** you want to use.
33
+
3.**Configure options** for each enricher (optional).
34
+
35
+
### Basic setup example
36
+
37
+
Here's a simple example showing how to set up log enrichment with process information:
The .NET enrichment framework provides some built-in enrichers, like:
56
+
57
+
-**[Process enricher](process-log-enricher.md)**: Process and thread information
58
+
59
+
## Custom enrichers
60
+
61
+
If the built-in enrichers don't meet your specific needs, you can create custom enrichers to add application-specific context. For more information, check [custom enrichment](custom-enricher.md).
description: Learn how to use the process log enricher in .NET.
4
+
ms.date: 10/10/2025
5
+
---
6
+
7
+
# Process log enricher
8
+
9
+
The process enricher augments telemetry logs with process-specific information.
10
+
11
+
You can register the enrichers in an IoC container. Then, all registered enrichers are picked up automatically by the respective telemetry instances, such as logs or metrics, where they enrich the telemetry information.
12
+
13
+
## Install the package
14
+
15
+
To get started, install the [📦 Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) NuGet package:
To use the process log enricher, first you enable enrichment. Then you can add the <xref:Microsoft.Extensions.DependencyInjection.ProcessEnricherServiceCollectionExtensions.AddProcessLogEnricher*> with default properties, as shown in the following code:
The <xref:Microsoft.Extensions.Diagnostics.Enrichment.ProcessLogEnricherOptions> class provides fine-grained control over which process-related properties are included in your log enrichment. This options class allows you to selectively enable or disable specific enrichment features such as process ID and thread ID information. Although default properties are supplied by the process enricher, you can customize them by initializing an instance of <xref:Microsoft.Extensions.Diagnostics.Enrichment.ProcessLogEnricherOptions> and providing it when registering the enricher.
51
+
52
+
You can enable or disable individual options of the enricher using <xref:Microsoft.Extensions.DependencyInjection.ProcessEnricherServiceCollectionExtensions.AddProcessLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.Diagnostics.Enrichment.ProcessLogEnricherOptions})>:
53
+
54
+
```csharp
55
+
serviceCollection.AddProcessLogEnricher(options=>
56
+
{
57
+
options.ThreadId=true;
58
+
options.ProcessId=true;
59
+
});
60
+
```
61
+
62
+
You may also disable or enable individual options using _appsettings.json_ file configuration, for example:
63
+
64
+
```json
65
+
{
66
+
"ProcessLogEnricherOptions": {
67
+
"ThreadId": true,
68
+
"ProcessId": true
69
+
}
70
+
}
71
+
```
72
+
73
+
and apply it accordingly using <xref:Microsoft.Extensions.DependencyInjection.ProcessEnricherServiceCollectionExtensions.AddProcessLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)>:
Copy file name to clipboardExpand all lines: docs/core/testing/microsoft-testing-platform-extensions-diagnostics.md
-6Lines changed: 0 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,9 +38,6 @@ You can also enable the diagnostics logs using the environment variables:
38
38
39
39
This extension allows you to create a crash dump file if the process crashes. This extension is shipped as part of [Microsoft.Testing.Extensions.CrashDump](https://nuget.org/packages/Microsoft.Testing.Extensions.CrashDump) NuGet package.
40
40
41
-
> [!IMPORTANT]
42
-
> The package is shipped with Microsoft .NET library closed-source free to use licensing model.
43
-
44
41
To configure the crash dump file generation, use the following options:
45
42
46
43
| Option | Description |
@@ -56,9 +53,6 @@ To configure the crash dump file generation, use the following options:
56
53
57
54
This extension allows you to create a dump file after a given timeout. This extension is shipped as part of [Microsoft.Testing.Extensions.HangDump](https://nuget.org/packages/Microsoft.Testing.Extensions.HangDump) package.
58
55
59
-
> [!IMPORTANT]
60
-
> The package is shipped with Microsoft .NET library closed-source free to use licensing model.
61
-
62
56
To configure the hang dump file generation, use the following options:
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/array-declaration-errors.md
+25Lines changed: 25 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,6 +37,11 @@ f1_keywords:
37
37
- "CS9208"
38
38
- "CS9209"
39
39
- "CS9210"
40
+
- "CS9212"
41
+
- "CS9213"
42
+
- "CS9214"
43
+
- "CS9215"
44
+
- "CS9222"
40
45
helpviewer_keywords:
41
46
- "CS0022"
42
47
- "CS0178"
@@ -73,6 +78,11 @@ helpviewer_keywords:
73
78
- "CS9208"
74
79
- "CS9209"
75
80
- "CS9210"
81
+
- "CS9212"
82
+
- "CS9213"
83
+
- "CS9214"
84
+
- "CS9215"
85
+
- "CS9222"
76
86
ms.date: 11/02/2023
77
87
---
78
88
# Resolve errors and warnings in array and collection declarations and initialization expressions
@@ -110,6 +120,11 @@ That's by design. The text closely matches the text of the compiler error / warn
110
120
-[**CS9188**](#invalid-collection-builder): *Type has a CollectionBuilderAttribute but no element type.*
111
121
-[**CS9203**](#invalid-collection-initializer): *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
112
122
-[**CS9210**](#invalid-collection-initializer): *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
123
+
-[**CS9212**](#invalid-collection-initializer): *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.*
124
+
-[**CS9213**](#invalid-collection-initializer): *Collection expression target 'type' has no element type.*
125
+
-[**CS9214**](#invalid-collection-initializer): *Collection expression type must have an applicable constructor that can be called with no arguments.*
126
+
-[**CS9215**](#invalid-collection-initializer): *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
127
+
-[**CS9222**](#invalid-collection-initializer): *Collection initializer results in an infinite chain of instantiations of collection 'type'.*
113
128
114
129
In addition, the following warnings are covered in this article:
115
130
@@ -141,6 +156,11 @@ The following errors indicate that the code generated by the compiler for a coll
141
156
-**CS9176**: *There is no target type for the collection literal.*
142
157
-**CS9203**: *A collection expression of this type cannot be used in this context because it may be exposed outside of the current scope.*
143
158
-**CS9210**: *This version of <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType>cannot be used with collection expressions.*
159
+
-**CS9212**: *Spread operator '`..`' cannot operate on variables of type 'type' because 'type' does not contain a public instance or extension definition for 'member'.*
160
+
-**CS9213**: *Collection expression target 'type' has no element type.*
161
+
-**CS9214**: *Collection expression type must have an applicable constructor that can be called with no arguments.*
162
+
-**CS9215**: *Collection expression type 'type' must have an instance or extension method 'Add' that can be called with a single argument.*
163
+
-**CS9222**: *Collection initializer results in an infinite chain of instantiations of collection 'type'.*
144
164
145
165
The compiler might also generate the following warning:
146
166
@@ -159,6 +179,11 @@ The errors all indicate that the code generated by the compiler for a collection
159
179
- Collection expressions can initialize explicitly typed variables of a collection type. If the variable isn't a collection or array type, or is implicitly typed (using `var`), a collection initializer can't be used.
160
180
- A `ref struct` type, like <xref:System.Span%601?displayProperty=nameWithType> can't be initialized with a collection expression that may violate ref safety.
161
181
- A collection expression can't correctly initialize an <xref:System.Collections.Immutable.ImmutableArray%601?displayProperty=nameWithType> using the current version. Use a different version of the runtime, or change the initialization expression.
182
+
- The spread operator (`..`) in **CS9212** requires the type to implement a suitable method (like `GetEnumerator`) to enumerate its elements. Ensure your type implements the required enumeration pattern or provides an extension method.
183
+
-**CS9213** occurs when the compiler can't determine what element type to use for the collection expression. This typically happens with custom collection types. Make sure your collection type properly exposes its element type through its type definition or implements appropriate collection interfaces.
184
+
-**CS9214** is generated when a collection expression tries to initialize a type that doesn't have a parameterless constructor. Collection expressions require a constructor that can be called with no arguments to create the instance before adding elements.
185
+
-**CS9215** happens when the collection type doesn't provide an `Add` method that accepts a single parameter of the element type. The `Add` method must be accessible (typically public) and accept exactly one argument that matches the collection's element type.
186
+
-**CS9222** indicates a circular dependency in collection initialization. This occurs when initializing a collection triggers the creation of another instance of the same collection type, which in turn requires initializing another instance, creating an infinite loop. Review your collection type's constructor and initialization logic to break the circular dependency.
162
187
163
188
The warnings indicates that the collection expression, including any [spread elements](../operators/collection-expressions.md#spread-element) might allocate memory. Creating different storage and converting might be more efficient.
0 commit comments