I have some code like this that goes in my Program.cs file:
builder.Services.AddHealthChecks().AddCheck<StartupProbe>("Startup Probe")
.AddCheck<LivenessProbe>("Liveness Probe")
.AddCheck<ReadinessProbe>("Readiness Probe");
It adds three IHealthCheck
classes to the check the health of my service while running in Kubernetes.
Most of the time, it works just fine. But sometimes it fails with TaskCanceledException
stating that "A task was canceled".
These health checks occur every 5 seconds or so, so a cancelled one every now and then is not really a big deal. But I need to suppress the message. They add up till my log files are full of them.
I read about using Exception Handling Middleware, but the docs are confusing me in the following ways:
It talks about loading "pages". I am working with a service. It does not have any UI (except for Swagger UI).
The Exception Handling Middleware docs seem to want me to decorate a controller with [atrributes] to make it work. But the
AddHealthChecks
method does not have a controller. It sets up the routes for me.I only want to handle the
TaskCanceledException
and (preferably) only for the health check calls. But I have not been able to see a way to limit how it handles exceptions.
I also saw the option of using an Exception Filter. But the docs for that say to use Exception Handling Middleware instead of that.
Is there a way to handle errors for my health checks? (And only my health checks)
NOTE: Here is the full exception I get, just in case it is useful:
"EventId": 1,
"LogLevel": "Error",
"Category": "Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware",
"Message": "An unhandled exception has occurred while executing the request.",
"Exception": "
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.Extensions.Diagnostics.HealthChecks.DefaultHealthCheckService.CheckHealthAsync(Func\u00602 predicate, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckMiddleware.InvokeAsync(HttpContext httpContext)
at Dynatrace.OneAgent.Introspection.Shared.OwinMiddlewareBase\u00601.Invoke(Context context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.HeaderPropagation.HeaderPropagationMiddleware.Invoke(HttpContext context)
at Dynatrace.OneAgent.Introspection.Shared.OwinMiddlewareBase\u00601.Invoke(Context context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)",
"State": {
"Message": "An unhandled exception has occurred while executing the request.",
"{OriginalFormat}": "An unhandled exception has occurred while executing the request."
}