-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Adding process log enricher documentation #49070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
8ef52af
doc updates
mariamgerges efdcba4
toc update
mariamgerges 502b1fe
fixes
mariamgerges 1ffb580
updates
mariamgerges f96db72
fixing reference
mariamgerges 817295a
fix
mariamgerges 28f4fb0
fixing
mariamgerges d7d9112
added overview and custom enricher
mariamgerges 94315d9
fix
mariamgerges 1a43657
fixes
mariamgerges 165d6c8
fix
mariamgerges e010186
fixing
mariamgerges 26ed855
fix again
mariamgerges 6b00300
adding nuget package reference
mariamgerges c05bb14
custom enricher
mariamgerges 26bcaa8
comments
mariamgerges 72b4f73
rename
mariamgerges cc0f599
fix
mariamgerges 0db22cc
Update custom-enricher.md
IEvangelist 68d667c
updates
mariamgerges 1042b3f
fixing code blocks
mariamgerges 7d8a156
fixing highlights
mariamgerges 3c36375
fix
mariamgerges 1c39fee
Update process-log-enricher.md
IEvangelist 9ae21a6
fix
mariamgerges 2abcd4e
suggestions
mariamgerges 4631857
suggestions
mariamgerges b4be17e
adding why in overview
mariamgerges fdf2866
fixes
mariamgerges fe2eb13
fix
mariamgerges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| --- | ||
| title: Custom log enricher | ||
| description: Learn how to use the custom log enricher in .NET. | ||
| ms.date: 10/13/2025 | ||
| --- | ||
|
|
||
| # Custom log enricher | ||
|
|
||
| You can easily create a custom enricher by creating a class that implements the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher> interface. | ||
| After the class is created, you register it with <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher)>. | ||
| Once registered, the logging infrastructure automatically calls the `Enrich()` method exactly once on every registered enricher for each log message produced. | ||
|
|
||
| ## Install the package | ||
|
|
||
| To get started, install the [📦 Microsoft.Extensions.Telemetry.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry.Abstractions) NuGet package: | ||
|
|
||
| ### [.NET CLI](#tab/dotnet-cli) | ||
|
|
||
| ```dotnetcli | ||
| dotnet add package Microsoft.Extensions.Telemetry.Abstractions | ||
| ``` | ||
|
|
||
| Or, if you're using .NET 10+ SDK: | ||
|
|
||
| ```dotnetcli | ||
| dotnet package add Microsoft.Extensions.Telemetry.Abstractions | ||
| ``` | ||
|
|
||
| ### [PackageReference](#tab/package-reference) | ||
|
|
||
| ```xml | ||
| <PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" | ||
| Version="*" /> <!-- Adjust version --> | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Implementation | ||
mariamgerges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Your custom enricher only needs to implement a single <xref:Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher.Enrich(Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector)> method. | ||
| 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 | ||
| the <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Add(System.String,System.Object)> method to record any properties it wants. | ||
|
|
||
| > [!NOTE] | ||
| > If your custom log enricher calls <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Add(System.String,System.Object)>, | ||
| > 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 | ||
| > to be sent further down the logging pipeline. | ||
|
|
||
| ```csharp | ||
| public class CustomEnricher : ILogEnricher | ||
| { | ||
| public void Enrich(IEnrichmentTagCollector collector) | ||
| { | ||
| collector.Add("customKey", "customValue"); | ||
| } | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| And you register it as shown in the following code using <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>: | ||
|
|
||
| ```csharp | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var builder = Host.CreateApplicationBuilder(args); | ||
| builder.Logging.EnableEnrichment(); | ||
| builder.Services.AddLogEnricher<CustomEnricher>(); | ||
| ``` | ||
|
|
||
| It's also possible to configure manual instantiation of custom enrichers: | ||
|
|
||
| ```csharp | ||
| public class AnotherEnricher : ILogEnricher | ||
| { | ||
| private readonly string _key; | ||
| private readonly object _value; | ||
| public CustomEnricher(string key, object value) | ||
| { | ||
| _key = key; | ||
| _value = value; | ||
| } | ||
| public void Enrich(IEnrichmentTagCollector collector) | ||
| { | ||
| collector.Add(_key, _value); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| 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)>: | ||
|
|
||
| ```csharp | ||
| var builder = Host.CreateApplicationBuilder(); | ||
| builder.Logging.EnableEnrichment(); | ||
| builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue")); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "EventId": 0, | ||
| "LogLevel": "Information", | ||
| "Category": "Enrichment.Program", | ||
| "Message": "This is a sample log message", | ||
| "State": { | ||
| "Message": "This is a sample log message", | ||
| "process.pid": "12924", | ||
| "thread.id": "2", | ||
| "{OriginalFormat}": "This is a sample log message" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "EventId": 0, | ||
| "LogLevel": "Information", | ||
| "Category": "Enrichment.Program", | ||
| "Message": "This is a sample log message", | ||
| "State": { | ||
| "Message": "This is a sample log message", | ||
| "process.pid": "10696", | ||
| "{OriginalFormat}": "This is a sample log message" | ||
| } | ||
| } |
mariamgerges marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| --- | ||
| title: Log enrichment overview | ||
| description: Learn about log enrichment in .NET and how to enhance your logs with contextual information. | ||
| ms.date: 10/13/2025 | ||
| --- | ||
|
|
||
| # Overview | ||
|
|
||
| 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. | ||
|
|
||
| ## What is enrichment? | ||
|
|
||
| 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. | ||
|
|
||
| ## Why is enrichment important? | ||
|
|
||
| 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. | ||
|
|
||
| ## How enrichment works | ||
|
|
||
| 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. | ||
|
|
||
| ## Dimension names and tags | ||
|
|
||
| Enrichers add information to telemetry using standardized dimension names (also called tags or keys). | ||
|
|
||
| ## Setting up enrichment | ||
|
|
||
| To use log enrichment in your application, you need to: | ||
|
|
||
| 1. **Enable enrichment** for logging. | ||
| 2. **Register specific enrichers** you want to use. | ||
| 3. **Configure options** for each enricher (optional). | ||
|
|
||
| ### Basic setup example | ||
|
|
||
| Here's a simple example showing how to set up log enrichment with process information: | ||
|
|
||
| :::code language="csharp" source="snippets/enrichment/Program.cs" highlight="8,9"::: | ||
|
|
||
| This configuration: | ||
|
|
||
| - Enables enrichment for logging via `EnableEnrichment()`. | ||
| - Registers the process log enricher via `AddProcessLogEnricher()`. | ||
| - Configures JSON console output to display the enriched data. | ||
|
|
||
| ### Output example | ||
|
|
||
| With enrichment enabled, your log output will automatically include additional contextual information: | ||
|
|
||
| :::code language="json" source="json-output.json" highlight="8"::: | ||
|
|
||
| ## Available enrichers | ||
|
|
||
| The .NET enrichment framework provides some built-in enrichers, like: | ||
|
|
||
| - **[Process enricher](process-log-enricher.md)**: Process and thread information | ||
|
|
||
| ## Custom enrichers | ||
|
|
||
| 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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| --- | ||
| title: Process log enricher | ||
| description: Learn how to use the process log enricher in .NET. | ||
| ms.date: 10/10/2025 | ||
mariamgerges marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| --- | ||
|
|
||
| # Process log enricher | ||
|
|
||
| The process enricher augments telemetry logs with process-specific information. | ||
|
|
||
| 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. | ||
|
|
||
| ## Install the package | ||
|
|
||
| To get started, install the [📦 Microsoft.Extensions.Telemetry](https://www.nuget.org/packages/Microsoft.Extensions.Telemetry) NuGet package: | ||
|
|
||
| ### [.NET CLI](#tab/dotnet-cli) | ||
|
|
||
| ```dotnetcli | ||
| dotnet add package Microsoft.Extensions.Telemetry | ||
| ``` | ||
|
|
||
| Or, if you're using .NET 10+ SDK: | ||
|
|
||
| ```dotnetcli | ||
| dotnet package add Microsoft.Extensions.Telemetry | ||
| ``` | ||
|
|
||
| ### [PackageReference](#tab/package-reference) | ||
|
|
||
| ```xml | ||
| <PackageReference Include="Microsoft.Extensions.Telemetry" | ||
| Version="*" /> <!-- Adjust version --> | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Usage | ||
|
|
||
| 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: | ||
|
|
||
| :::code language="csharp" source="snippets/enrichment/Program.cs" highlight="8,9"::: | ||
|
|
||
| Given this code sample, the output should be similar to the following JSON: | ||
|
|
||
| :::code language="json" source="json-output.json" highlight="8"::: | ||
|
|
||
| ## `ProcessLogEnricherOptions` | ||
|
|
||
| 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. | ||
|
|
||
| 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})>: | ||
|
|
||
| ```csharp | ||
| serviceCollection.AddProcessLogEnricher(options => | ||
| { | ||
| options.ThreadId = true; | ||
| options.ProcessId = true; | ||
| }); | ||
| ``` | ||
|
|
||
| You may also disable or enable individual options using _appsettings.json_ file configuration, for example: | ||
|
|
||
| ```json | ||
| { | ||
| "ProcessLogEnricherOptions": { | ||
| "ThreadId": true, | ||
| "ProcessId": true | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| and apply it accordingly using <xref:Microsoft.Extensions.DependencyInjection.ProcessEnricherServiceCollectionExtensions.AddProcessLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)>: | ||
|
|
||
| ```csharp | ||
| serviceCollection.AddProcessLogEnricher( | ||
| hostBuilder.Configuration.GetSection("ProcessLogEnricherOptions")); | ||
| ``` | ||
|
|
||
| The console output after enabling both options should look like this: | ||
|
|
||
| :::code language="json" source="json-output-all-enabled.json" highlight="8,9"::: | ||
|
|
||
| ## Default configuration | ||
|
|
||
| The default configuration for process log enrichment is: | ||
|
|
||
| | Property | Default Value | Description | | ||
| |-------------|---------------|---------------------------------------------------------| | ||
| | `ProcessId` | `true` | If true, logs are enriched with the current process ID. | | ||
| | `ThreadId` | `false` | If true, logs are enriched with the current thread ID | | ||
17 changes: 17 additions & 0 deletions
17
docs/core/enrichment/snippets/enrichment/Enrichment.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.9" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.9" /> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.9" /> | ||
| <PackageReference Include="Microsoft.Extensions.Telemetry" Version="9.9.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using System.Text.Json; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| var builder = Host.CreateApplicationBuilder(args); | ||
|
|
||
| builder.Logging.EnableEnrichment(); | ||
| builder.Services.AddProcessLogEnricher(); | ||
|
|
||
| builder.Logging.AddJsonConsole(op => | ||
| { | ||
| op.JsonWriterOptions = new JsonWriterOptions | ||
| { | ||
| Indented = true | ||
| }; | ||
| }); | ||
|
|
||
| var host = builder.Build(); | ||
| var logger = host.Services.GetRequiredService<ILogger<Program>>(); | ||
|
|
||
| logger.LogInformation("This is a sample log message"); | ||
| await host.RunAsync(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.