ASP.NET Core › Health Checks
How would you exclude health check endpoints from other cross-cutting middleware like authentication, logging, or rate limiting that shouldn't apply to frequent automated health polling?
IntermediateAnswer
Since health endpoints are typically polled very frequently by automated infrastructure (every few seconds by a load balancer or orchestrator), it's common to exclude them from middleware not relevant to that traffic -- achieved by ordering MapHealthChecks appropriately relative to other middleware registrations, or by explicitly checking and skipping the health check path within custom middleware, preventing noisy logs or unnecessary rate-limit consumption from routine health polling.
app.UseWhen(
context => !context.Request.Path.StartsWithSegments("/health"),
appBuilder => appBuilder.UseMiddleware<RequestLoggingMiddleware>()
);
app.MapHealthChecks("/health");Real-world example A team notices their centralized logging system is flooded with thousands of routine health-check log entries per hour, and fixes it by wrapping their logging middleware in UseWhen to skip the /health path entirely, dramatically reducing log noise without losing any meaningful observability.