Skip to content

Commit 07409f6

Browse files
authored
Merge pull request #36882 from dotnet/main
Merge to Live
2 parents 4d51d18 + 1e4d1ae commit 07409f6

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
---
2+
title: Enrich HTTP request logs in ASP.NET Core
3+
description: Learn how to enrich incoming HTTP request logs with custom data using the IHttpLogEnricher interface in ASP.NET Core.
4+
ai-usage: ai-assisted
5+
author: mariamaziz
6+
monikerRange: '>= aspnetcore-8.0'
7+
ms.author: tdykstra
8+
ms.custom: mvc
9+
ms.date: 03/09/2026
10+
uid: fundamentals/http-logging/http-log-enricher
11+
---
12+
13+
# Enrich HTTP request logs in ASP.NET Core
14+
15+
[!INCLUDE[](~/includes/not-latest-version-without-not-supported-content.md)]
16+
17+
:::moniker range=">= aspnetcore-8.0"
18+
19+
You can create a custom HTTP log enricher by creating a class that implements the <xref:Microsoft.AspNetCore.Diagnostics.Logging.IHttpLogEnricher> interface. Unlike general-purpose log enrichers that enrich all logs in your application, HTTP log enrichers specifically target incoming HTTP request logs in ASP.NET Core, allowing you to add contextual information based on the `HttpContext` of each request.
20+
21+
After the class is created, you register it with <xref:Microsoft.Extensions.DependencyInjection.HttpLoggingServiceCollectionExtensions.AddHttpLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>. Once registered, the logging infrastructure automatically calls the `Enrich()` method on every registered enricher for each incoming HTTP request processed by the ASP.NET Core pipeline.
22+
23+
> [!IMPORTANT]
24+
> The `IHttpLogEnricher` interface is experimental and requires the `EXTEXP0013` diagnostic ID suppression. For more information, see [Experimental features in .NET Extensions](https://aka.ms/dotnet-extensions-warnings/EXTEXP0013).
25+
26+
## Install the package
27+
28+
To get started, install the [Microsoft.AspNetCore.Diagnostics.Middleware](https://www.nuget.org/packages/Microsoft.AspNetCore.Diagnostics.Middleware) NuGet package:
29+
30+
### [.NET CLI](#tab/dotnet-cli)
31+
32+
```dotnetcli
33+
dotnet add package Microsoft.AspNetCore.Diagnostics.Middleware
34+
```
35+
36+
Or, if you're using .NET 10+ SDK:
37+
38+
```dotnetcli
39+
dotnet package add Microsoft.AspNetCore.Diagnostics.Middleware
40+
```
41+
42+
### [PackageReference](#tab/package-reference)
43+
44+
```xml
45+
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Middleware"
46+
Version="*" /> <!-- Adjust version -->
47+
```
48+
49+
---
50+
51+
## IHttpLogEnricher implementation
52+
53+
Your custom HTTP log enricher needs to implement a single <xref:Microsoft.AspNetCore.Diagnostics.Logging.IHttpLogEnricher.Enrich(Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector,Microsoft.AspNetCore.Http.HttpContext)> method. During enrichment, this method is called and given an <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector> instance along with the <xref:Microsoft.AspNetCore.Http.HttpContext> for the incoming request. 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.
54+
55+
> [!NOTE]
56+
> If your custom HTTP log enricher calls <xref:Microsoft.Extensions.Diagnostics.Enrichment.IEnrichmentTagCollector.Add(System.String,System.Object)>,
57+
> it's acceptable to send any type of argument to the `value` parameter as-is, because it's parsed into the actual type and serialized internally
58+
> to be sent further down the logging pipeline.
59+
60+
```csharp
61+
using Microsoft.AspNetCore.Diagnostics.Logging;
62+
using Microsoft.Extensions.Diagnostics.Enrichment;
63+
64+
public class CustomHttpLogEnricher : IHttpLogEnricher
65+
{
66+
public void Enrich(IEnrichmentTagCollector collector, HttpContext httpContext)
67+
{
68+
// Add custom tags based on the incoming HTTP request
69+
collector.Add("request_method", httpContext.Request.Method);
70+
collector.Add("request_scheme", httpContext.Request.Scheme);
71+
72+
// Add tags based on the response status code (available during the response phase)
73+
collector.Add("response_status_code", httpContext.Response.StatusCode);
74+
75+
// Add tags based on user authentication status
76+
if (httpContext.User?.Identity?.IsAuthenticated is true)
77+
{
78+
collector.Add("user_authenticated", true);
79+
}
80+
}
81+
}
82+
83+
```
84+
And you register it as shown in the following code using <xref:Microsoft.Extensions.DependencyInjection.HttpLoggingServiceCollectionExtensions.AddHttpLogEnricher``1(Microsoft.Extensions.DependencyInjection.IServiceCollection)>:
85+
86+
```csharp
87+
88+
using System.Text.Json;
89+
90+
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
91+
92+
builder.Services.AddHttpLogEnricher<CustomHttpLogEnricher>();
93+
builder.Services.AddRedaction();
94+
95+
builder.Logging.AddJsonConsole(op =>
96+
{
97+
op.JsonWriterOptions = new JsonWriterOptions
98+
{
99+
Indented = true
100+
};
101+
});
102+
103+
WebApplication app = builder.Build();
104+
105+
app.UseHttpLogging();
106+
107+
app.MapGet("/", () => "Hello, World!");
108+
109+
await app.RunAsync();
110+
111+
```
112+
113+
## Key differences from general log enrichers
114+
115+
HTTP log enrichers differ from general-purpose log enrichers (<xref:Microsoft.Extensions.Diagnostics.Enrichment.ILogEnricher>) in several important ways:
116+
117+
- **Scope**: HTTP log enrichers only enrich logs produced by incoming ASP.NET Core HTTP requests, while general log enrichers enrich all logs in the application.
118+
- **Context**: HTTP log enrichers have access to the full `HttpContext`, including the request, response, user, connection, and any other context data associated with the incoming request.
119+
- **Package**: HTTP log enrichers require the `Microsoft.AspNetCore.Diagnostics.Middleware` package, while general log enrichers use the `Microsoft.Extensions.Telemetry.Abstractions` package.
120+
- **Direction**: HTTP log enrichers target **incoming** server-side requests, while <xref:Microsoft.Extensions.Http.Logging.IHttpClientLogEnricher> targets **outgoing** client-side HTTP requests.
121+
122+
## Remarks
123+
124+
- The `Enrich` method is called during the HTTP response phase of the request/response lifecycle, after the response has been processed.
125+
- The `httpContext` parameter is always provided and will never be `null`.
126+
- Multiple enrichers can be registered and will be executed in the order they were registered.
127+
- If an enricher throws an exception, it's logged and execution continues with the remaining enrichers.
128+
- The `IHttpLogEnricher` interface is marked as experimental with diagnostic ID `EXTEXP0013` and requires .NET 8 or later.
129+
- Calling `AddHttpLogEnricher<T>()` automatically sets up the required HTTP logging redaction infrastructure by internally calling `AddHttpLoggingRedaction()`.
130+
- You must still add the `UseHttpLogging()` middleware in the application pipeline for HTTP logs to be emitted.
131+
132+
## See also
133+
134+
- [HTTP logging in ASP.NET Core](~/fundamentals/http-logging/index.md)
135+
136+
:::moniker-end

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ items:
508508
uid: fundamentals/logging/index
509509
- name: HTTP logging
510510
uid: fundamentals/http-logging/index
511+
- name: Enrich HTTP request logs in ASP.NET Core
512+
uid: fundamentals/http-logging/http-log-enricher
511513
- name: W3C logger
512514
uid: fundamentals/w3c-logger/index
513515
- name: Health checks

0 commit comments

Comments
 (0)