Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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 NuGet package:
dotnet add package Microsoft.Extensions.Telemetry
Or, if you're using .NET 10+ SDK:
dotnet package add Microsoft.Extensions.Telemetry
Usage
To use the process log enricher, first you enable enrichment. Then you can add the AddProcessLogEnricher with default properties, as shown in the following code:
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();
Given this code sample, the output should be similar to the following JSON:
{
"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"
}
}
ProcessLogEnricherOptions
The 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 ProcessLogEnricherOptions and providing it when registering the enricher.
You can enable or disable individual options of the enricher using AddProcessLogEnricher(IServiceCollection, Action<ProcessLogEnricherOptions>):
serviceCollection.AddProcessLogEnricher(options =>
{
options.ThreadId = true;
options.ProcessId = true;
});
You may also disable or enable individual options using appsettings.json file configuration, for example:
{
"ProcessLogEnricherOptions": {
"ThreadId": true,
"ProcessId": true
}
}
and apply it accordingly using AddProcessLogEnricher(IServiceCollection, IConfigurationSection):
serviceCollection.AddProcessLogEnricher(
hostBuilder.Configuration.GetSection("ProcessLogEnricherOptions"));
The console output after enabling both options should look like this:
{
"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"
}
}
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 |