diff --git a/src/SchoolAccount.Collect.Api/Extensions/MiddlewareExtensions.cs b/src/SchoolAccount.Collect.Api/Extensions/MiddlewareExtensions.cs index 1504346..44ed769 100644 --- a/src/SchoolAccount.Collect.Api/Extensions/MiddlewareExtensions.cs +++ b/src/SchoolAccount.Collect.Api/Extensions/MiddlewareExtensions.cs @@ -1,4 +1,6 @@ -using SchoolAccount.Collect.Api.Middleware; +using SchoolAccount.Collect.Api.Middleware; +using Serilog; +using Serilog.Events; namespace SchoolAccount.Collect.Api.Extensions; @@ -10,4 +12,37 @@ public static IApplicationBuilder UseRequestContextLogging(this IApplicationBuil return app; } + + public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder app) + { + app.UseSerilogRequestLogging(options => options.GetLevel = GetRequestLogLevel); + + return app; + } + + // Decides how loudly to record each request summary. + // + // The container platform polls the health endpoint constantly, and a summary line per probe + // buries everything else in the log stream. Those drop to Verbose, which nothing collects, + // so they cost nothing but can be switched on if the probes themselves need investigating. + // + // Everything else keeps Serilog's own defaults: failures at Error, ordinary requests at + // Information. The status code is checked as well as the exception because the global + // exception handler deals with most failures before they reach this middleware, so a 500 + // usually arrives here with no exception attached. + private static LogEventLevel GetRequestLogLevel( + HttpContext context, + double elapsedMilliseconds, + Exception? exception + ) + { + if (exception is not null || context.Response.StatusCode >= 500) + { + return LogEventLevel.Error; + } + + return context.Request.Path.StartsWithSegments("/health") + ? LogEventLevel.Verbose + : LogEventLevel.Information; + } } diff --git a/src/SchoolAccount.Collect.Api/Program.cs b/src/SchoolAccount.Collect.Api/Program.cs index 65ae400..0f1f43a 100644 --- a/src/SchoolAccount.Collect.Api/Program.cs +++ b/src/SchoolAccount.Collect.Api/Program.cs @@ -38,7 +38,7 @@ app.UseRequestContextLogging(); -app.UseSerilogRequestLogging(); +app.UseRequestLogging(); app.UseExceptionHandler();