|
| 1 | +--- |
| 2 | +title: Custom log enricher |
| 3 | +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: |
| 16 | + |
| 17 | +### [.NET CLI](#tab/dotnet-cli) |
| 18 | + |
| 19 | +```dotnetcli |
| 20 | +dotnet add package Microsoft.Extensions.Telemetry.Abstractions |
| 21 | +``` |
| 22 | + |
| 23 | +Or, if you're using .NET 10+ SDK: |
| 24 | + |
| 25 | +```dotnetcli |
| 26 | +dotnet package add Microsoft.Extensions.Telemetry.Abstractions |
| 27 | +``` |
| 28 | + |
| 29 | +### [PackageReference](#tab/package-reference) |
| 30 | + |
| 31 | +```xml |
| 32 | +<PackageReference Include="Microsoft.Extensions.Telemetry.Abstractions" |
| 33 | + Version="*" /> <!-- Adjust version --> |
| 34 | +``` |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +## Implementation |
| 39 | + |
| 40 | +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 |
| 47 | +> to be sent further down the logging pipeline. |
| 48 | +
|
| 49 | +```csharp |
| 50 | +public class CustomEnricher : ILogEnricher |
| 51 | +{ |
| 52 | + public void Enrich(IEnrichmentTagCollector collector) |
| 53 | + { |
| 54 | + collector.Add("customKey", "customValue"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +And you register it as shown in the following code using <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>: |
| 61 | + |
| 62 | +```csharp |
| 63 | +var builder = Host.CreateApplicationBuilder(args); |
| 64 | +builder.Logging.EnableEnrichment(); |
| 65 | +builder.Services.AddLogEnricher<CustomEnricher>(); |
| 66 | +``` |
| 67 | + |
| 68 | +It's also possible to configure manual instantiation of custom enrichers: |
| 69 | + |
| 70 | +```csharp |
| 71 | +public class AnotherEnricher : ILogEnricher |
| 72 | +{ |
| 73 | + private readonly string _key; |
| 74 | + private readonly object _value; |
| 75 | + public CustomEnricher(string key, object value) |
| 76 | + { |
| 77 | + _key = key; |
| 78 | + _value = value; |
| 79 | + } |
| 80 | + public void Enrich(IEnrichmentTagCollector collector) |
| 81 | + { |
| 82 | + collector.Add(_key, _value); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +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)>: |
| 88 | + |
| 89 | +```csharp |
| 90 | +var builder = Host.CreateApplicationBuilder(); |
| 91 | +builder.Logging.EnableEnrichment(); |
| 92 | +builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue")); |
| 93 | +``` |
0 commit comments