Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
77 changes: 73 additions & 4 deletions docs/core/enrichment/application-log-enricher.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,38 @@ You can provide additional configuration via `appsettings.json`. There are two p

#### 3. Register the service log enricher

Register the log enricher into the dependency injection container using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:
Register the log enricher into the dependency injection container:

### [.NET 10+](#tab/net10-plus)

Starting with .NET 10, use the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddApplicationLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method:

```csharp
serviceCollection.AddApplicationLogEnricher();
```

You can enable or disable individual options of the enricher:

```csharp
serviceCollection.AddApplicationLogEnricher(options =>
{
options.BuildVersion = true;
options.DeploymentRing = true;
});
```

### [.NET 9 and earlier](#tab/net9-earlier)

For .NET 9 and earlier versions, use the <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection)> method:

> [!WARNING]
> The `AddServiceLogEnricher` method is obsolete starting with .NET 10. Use `AddApplicationLogEnricher` instead.

```csharp
serviceCollection.AddServiceLogEnricher();
```

You can enable or disable individual options of the enricher using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action{Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions})>:
You can enable or disable individual options of the enricher:

```csharp
serviceCollection.AddServiceLogEnricher(options =>
Expand All @@ -93,18 +118,30 @@ serviceCollection.AddServiceLogEnricher(options =>
});
```

---

Alternatively, configure options using `appsettings.json`:

:::code language="json" source="snippets/servicelogenricher/appsettings.json" range="8-11":::

And apply the configuration using <xref:Microsoft.Extensions.DependencyInjection.ApplicationEnricherServiceCollectionExtensions.AddServiceLogEnricher(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.Extensions.Configuration.IConfigurationSection)>:
And apply the configuration:

### [.NET 10+](#tab/net10-plus-config)

```csharp
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
```

### [.NET 9 and earlier](#tab/net9-earlier-config)

```csharp
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddServiceLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));
```

---

### `ApplicationLogEnricherOptions` Configuration options

The service log enricher supports several configuration options through the <xref:Microsoft.Extensions.Diagnostics.Enrichment.ApplicationLogEnricherOptions> class:
Expand All @@ -128,8 +165,40 @@ Here's a complete example showing how to set up the service log enricher:

**Program.cs:**

### [.NET 10+](#tab/net10-plus-config)

```csharp
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);
builder.UseApplicationMetadata();
builder.Logging.EnableEnrichment();
builder.Logging.AddJsonConsole(op =>
{
op.JsonWriterOptions = new JsonWriterOptions
{
Indented = true
};
});
builder.Services.AddApplicationLogEnricher(builder.Configuration.GetSection("ApplicationLogEnricherOptions"));

var host = builder.Build();
var logger = host.Services.GetRequiredService<ILogger<Program>>();

logger.LogInformation("This is a sample log message");

await host.RunAsync();
```

### [.NET 9 and earlier](#tab/net9-earlier-config)

:::code language="csharp" source="snippets/servicelogenricher/Program.cs" :::

---

### Enriched log output

With the service log enricher configured, your log output will include service-specific dimensions:
Expand Down
25 changes: 24 additions & 1 deletion docs/core/enrichment/custom-enricher.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dotnet package add Microsoft.Extensions.Telemetry.Abstractions

---

## Implementation
## `ILogEnricher` Implementation

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
Expand Down Expand Up @@ -91,3 +91,26 @@ var builder = Host.CreateApplicationBuilder();
builder.Logging.EnableEnrichment();
builder.Services.AddLogEnricher(new AnotherEnricher("anotherKey", "anotherValue"));
```

## Static log enrichers

.NET provides <xref:Microsoft.Extensions.Diagnostics.Enrichment.IStaticLogEnricher> to enrich logs with immutable properties throughout the application lifecycle. Static enrichers execute once during startup initialization, delivering significant performance benefits over standard enrichers that execute for each log record. This approach is particularly advantageous in high-throughput logging scenarios, as it eliminates the overhead of redundant enrichment operations for data that remains constant, that doesn't change throughout the service's lifetime.

```csharp
public class StaticEnricher : IStaticLogEnricher
{
public void Enrich(IEnrichmentTagCollector collector)
{
collector.Add("app.version", "1.0.0");
collector.Add("environment", "production");
}
}
```

And you register it as shown in the following code <xref:Microsoft.Extensions.DependencyInjection.EnrichmentServiceCollectionExtensions.AddStaticLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:

```csharp
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.EnableEnrichment();
builder.Services.AddStaticLogEnricher<StaticEnricher>();
```
Loading