title: Handling Errors in Health Checks
body: <p>I have some code like this that goes in my Program.cs file:</p>
  <pre><code>builder.Services.AddHealthChecks().AddCheck&lt;StartupProbe&gt;(&quot;Startup Probe&quot;)
      .AddCheck&lt;LivenessProbe&gt;(&quot;Liveness Probe&quot;)
      .AddCheck&lt;ReadinessProbe&gt;(&quot;Readiness Probe&quot;);
  </code></pre>
  <p>It adds three <code>IHealthCheck</code> classes to the <a href="https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0" rel="nofollow noreferrer">check the health</a> of my service while running in Kubernetes.</p>
  <p>Most of the time, it works just fine.  But sometimes it fails with <code>TaskCanceledException</code> stating that &quot;A task was canceled&quot;.</p>
  <p>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.</p>
  <p>I read about using <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-6.0" rel="nofollow noreferrer">Exception Handling Middleware</a>, but the docs are confusing me in the following ways:</p>
  <ol>
  <li><p>It talks about loading &quot;pages&quot;.  I am working with a service.  It does not have any UI (except for Swagger UI).</p>
  </li>
  <li><p>The Exception Handling Middleware docs seem to want me to decorate a controller with [atrributes] to make it work.  But the <code>AddHealthChecks</code> method does not have a controller.  It sets up the routes for me.</p>
  </li>
  <li><p>I only want to handle the <code>TaskCanceledException</code> and (preferably) only for the health check calls.  But I have not been able to see a way to limit how it handles exceptions.</p>
  </li>
  </ol>
  <p>I also saw the option of using an Exception Filter.  But the docs for that say to use Exception Handling Middleware instead of that.</p>
  <p><strong>Is there a way to handle errors for my health checks?</strong>  (And only my health checks)</p>
  <p>NOTE: Here is the full exception I get, just in case it is useful:</p>
  <pre><code>&quot;EventId&quot;: 1,
  &quot;LogLevel&quot;: &quot;Error&quot;,
  &quot;Category&quot;: &quot;Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware&quot;,
  &quot;Message&quot;: &quot;An unhandled exception has occurred while executing the request.&quot;,
  &quot;Exception&quot;: &quot;
      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)&quot;,
  &quot;State&quot;: {
      &quot;Message&quot;: &quot;An unhandled exception has occurred while executing the request.&quot;,
      &quot;{OriginalFormat}&quot;: &quot;An unhandled exception has occurred while executing the request.&quot;
  }
  </code></pre>