diff --git a/fundamentals/httplogging/httplogenricher/CustomHttpLogEnricher.cs b/fundamentals/httplogging/httplogenricher/CustomHttpLogEnricher.cs new file mode 100644 index 0000000..fcaa5e8 --- /dev/null +++ b/fundamentals/httplogging/httplogenricher/CustomHttpLogEnricher.cs @@ -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); + } + } +} diff --git a/fundamentals/httplogging/httplogenricher/Program.cs b/fundamentals/httplogging/httplogenricher/Program.cs new file mode 100644 index 0000000..95b7f4c --- /dev/null +++ b/fundamentals/httplogging/httplogenricher/Program.cs @@ -0,0 +1,24 @@ +#pragma warning disable EXTEXP0013 + +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(); diff --git a/fundamentals/httplogging/httplogenricher/appsettings.json b/fundamentals/httplogging/httplogenricher/appsettings.json new file mode 100644 index 0000000..e2c6519 --- /dev/null +++ b/fundamentals/httplogging/httplogenricher/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Warning", + "Microsoft.Hosting.Lifetime": "Warning", + "Microsoft.AspNetCore.Watch.BrowserRefresh": "Warning", + "Microsoft.AspNetCore.HttpLogging.HttpLoggingMiddleware": "Information" + } + } +} diff --git a/fundamentals/httplogging/httplogenricher/httplogenricher.csproj b/fundamentals/httplogging/httplogenricher/httplogenricher.csproj new file mode 100644 index 0000000..df65e28 --- /dev/null +++ b/fundamentals/httplogging/httplogenricher/httplogenricher.csproj @@ -0,0 +1,16 @@ + + + + Exe + net10.0 + enable + true + httplogenricher + + + + + + + +