Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 23 additions & 0 deletions fundamentals/httplogging/httplogenricher/CustomHttpLogEnricher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma warning disable EXTEXP0013

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);
}
}
}
24 changes: 24 additions & 0 deletions fundamentals/httplogging/httplogenricher/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma warning disable EXTEXP0013

using System.Text.Json;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpLogEnricher<CustomHttpLogEnricher>();
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();
10 changes: 10 additions & 0 deletions fundamentals/httplogging/httplogenricher/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.Hosting.Lifetime": "Warning",
"Microsoft.AspNetCore.Watch.BrowserRefresh": "Warning",
"Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information"
}
}
}
16 changes: 16 additions & 0 deletions fundamentals/httplogging/httplogenricher/httplogenricher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<RootNamespace>httplogenricher</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.Middleware" Version="10.2.0" />
<PackageReference Include="Microsoft.Extensions.Compliance.Redaction" Version="10.2.0" />
</ItemGroup>

</Project>