dotnet / dotnet/aspnetcore

Provide AddChecks method in IHealthCheckBuilder

Open
#32,317 4 comments 1 reaction 0 assignees View on GitHub
api-suggestion area-healthchecks enhancement
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

## Background and Motivation

Currently IHealthCheckBuilder provides a "AddCheck" method to register a custom health check in my application.

`services.AddHealthChecks().AddCheck("CustomHealthCheck");`

And inline is implementation for CheckHealthAsync method in my CustomHealthCheck class.

```
public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
//hardcoding directly the result for explanation purpose
return Task.FromResult(new HealthCheckResult(HealthStatus.Healthy));
}
```
We can observe that I am able to return only one HealthCheckResult object from my CustomHealthCheck class.
I am looking for an api from which I can return multiple health check results from a single custom health check class.
Into this custom health check class I would like to add and remove health checks from an external application while the application is running so that there would be no need to modify code and redeploy the application.

## Proposed API

`services.AddHealthChecks().AddChecks("CustomHealthChecks");`

And inline is implementation for CheckHealthAsync method in my CustomHealthChecks class.
The interface would be IHealthChecks instead of IHealthCheck which returns a list of HealthCheckResults.

```
public Task> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
//hardcoding result for explanation
return Task.FromResult(new List(){new HealthCheckResult(
HealthStatus.Healthy)});
}
```

## Usage Examples

`services.AddHealthChecks().AddChecks("CustomHealthChecks");`

With this registration in my ConfigureServices method I can add 0 to n healthchecks in my CustomHealthChecks class during runtime and return their status by populating the list.

## Alternative Designs

Currently I am populating the IReadOnlyDictionary Data of HealthCheckResult class to meet the requirement of different health checks status added dynamically during runtime.

## Risks

No risk is involved because this is new api.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.