Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
65 changes: 65 additions & 0 deletions docs/core/enrichment/process-log-enricher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
title: Process log enricher
description: Learn how to use the process log enricher in .NET.
ms.date: 10/10/2025
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the coding guidelines, this appears to be AI-generated content. The document should include the ai-usage frontmatter key with appropriate value (ai-generated or ai-assisted).

Copilot generated this review using guidance from repository custom instructions.

---

# 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.

## Usage

To be able to use the process log enricher, first you need to enable enrichment like this:
:::code language="csharp" source="snippets/enrichment/Program.cs" highlight="15":::

then you can add the <xref:Microsoft.Extensions.DependencyInjection.ProcessEnricherServiceCollectionExtensions.AddProcessLogEnricher*> with default properties, like this:

:::code language="csharp" source="snippets/enrichment/Program.cs" highlight="16":::

or alternatively:

```cs
var hostApplicationBuilder = WebApplication.CreateBuilder();
hostApplicationBuilder.Services.AddProcessLogEnricher();
```

Or, optionally, 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})>:

```cs
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)>:

```cs
serviceCollection.AddProcessLogEnricher(hostBuilder.Configuration.GetSection("ProcessLogEnricherOptions"));
```

## Default configuration

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.

The default configuration for 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 docs/core/enrichment/snippets/enrichment/Enrichment.csproj
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>
12 changes: 12 additions & 0 deletions docs/core/enrichment/snippets/enrichment/Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved.

using Microsoft.Extensions.Logging;

namespace Enrichment
{
internal static partial class Log
{
[LoggerMessage(LogLevel.Information, "This is a sample log message")]
public static partial void LogSampleMessage(this ILogger logger);
}
}
38 changes: 38 additions & 0 deletions docs/core/enrichment/snippets/enrichment/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Enrichment
{
internal class Program
{
public static async Task Main()
{
var host = new HostBuilder()
.ConfigureLogging((hostingContext, loggingBuilder) =>
{
loggingBuilder.EnableEnrichment();
loggingBuilder.Services.AddProcessLogEnricher();
loggingBuilder.AddJsonConsole(op =>
{
op.JsonWriterOptions = new JsonWriterOptions
{
Indented = true
};
});
});

var hostBuilder = host.Build();
var logger =
hostBuilder.Services
.GetRequiredService<ILoggerFactory>()
.CreateLogger<Program>();

logger.LogSampleMessage();

await hostBuilder.RunAsync();

}
}
}
4 changes: 4 additions & 0 deletions docs/navigate/tools-diagnostics/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,10 @@ items:
href: ../../core/diagnostics/distributed-tracing-collection-walkthroughs.md
- name: Built-in activities
href: ../../core/diagnostics/distributed-tracing-builtin-activities.md
- name: Enrichment
items:
- name: Process log enricher
href: ../../core/enrichment/process-log-enricher.md
- name: Specialized diagnostics
items:
- name: Overview
Expand Down