diff --git a/aspnetcore/fundamentals/http-logging/http-log-enricher.md b/aspnetcore/fundamentals/http-logging/http-log-enricher.md index b5193af72930..4fa8c632658c 100644 --- a/aspnetcore/fundamentals/http-logging/http-log-enricher.md +++ b/aspnetcore/fundamentals/http-logging/http-log-enricher.md @@ -5,8 +5,9 @@ ai-usage: ai-assisted author: mariamaziz monikerRange: '>= aspnetcore-8.0' ms.author: tdykstra +ms.reviewer: tdykstra ms.custom: mvc -ms.date: 03/09/2026 +ms.date: 06/08/2026 uid: fundamentals/http-logging/http-log-enricher --- @@ -33,12 +34,6 @@ To get started, install the [Microsoft.AspNetCore.Diagnostics.Middleware](https: dotnet add package Microsoft.AspNetCore.Diagnostics.Middleware ``` -Or, if you're using .NET 10+ SDK: - -```dotnetcli -dotnet package add Microsoft.AspNetCore.Diagnostics.Middleware -``` - ### [PackageReference](#tab/package-reference) ```xml @@ -57,58 +52,11 @@ Your custom HTTP log enricher needs to implement a single 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 > to be sent further down the logging pipeline. -```csharp -using Microsoft.AspNetCore.Diagnostics.Logging; -using Microsoft.Extensions.Diagnostics.Enrichment; - -public class CustomHttpLogEnricher : IHttpLogEnricher -{ - public void Enrich(IEnrichmentTagCollector collector, HttpContext httpContext) - { - // Add custom tags based on the incoming HTTP request - collector.Add("request_method", httpContext.Request.Method); - collector.Add("request_scheme", httpContext.Request.Scheme); - - // Add tags based on the response status code (available during the response phase) - collector.Add("response_status_code", httpContext.Response.StatusCode); - - // Add tags based on user authentication status - if (httpContext.User?.Identity?.IsAuthenticated is true) - { - collector.Add("user_authenticated", true); - } - } -} +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/httplogging/httplogenricher/CustomHttpLogEnricher.cs"::: -``` And you register it as shown in the following code using : -```csharp - -using System.Text.Json; - -WebApplicationBuilder builder = WebApplication.CreateBuilder(args); - -builder.Services.AddHttpLogEnricher(); -builder.Services.AddRedaction(); - -builder.Logging.AddJsonConsole(op => -{ - op.JsonWriterOptions = new JsonWriterOptions - { - Indented = true - }; -}); - -WebApplication app = builder.Build(); - -app.UseHttpLogging(); - -app.MapGet("/", () => "Hello, World!"); - -await app.RunAsync(); - -``` +:::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/httplogging/httplogenricher/Program.cs" highlight="7, 20"::: ## Key differences from general log enrichers @@ -131,6 +79,7 @@ HTTP log enrichers differ from general-purpose log enrichers (