Skip to content
Draft
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
37 changes: 36 additions & 1 deletion src/SchoolAccount.Collect.Api/Extensions/MiddlewareExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SchoolAccount.Collect.Api.Middleware;
using SchoolAccount.Collect.Api.Middleware;
using Serilog;
using Serilog.Events;

namespace SchoolAccount.Collect.Api.Extensions;

Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/SchoolAccount.Collect.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

app.UseRequestContextLogging();

app.UseSerilogRequestLogging();
app.UseRequestLogging();

app.UseExceptionHandler();

Expand Down
Loading