Skip to content

Instantly share code, notes, and snippets.

@scottsauber
Last active May 22, 2017 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottsauber/948c96099e3fd50dc9725b7256b0ed27 to your computer and use it in GitHub Desktop.
Save scottsauber/948c96099e3fd50dc9725b7256b0ed27 to your computer and use it in GitHub Desktop.
[Route("api/[controller]")]
public class HealthCheckController : Controller
{
private readonly IHealthCheckService _healthCheckService;
public HealthCheckController(IHealthCheckService healthCheckService)
{
_healthCheckService = healthCheckService;
}
[HttpGet]
public async Task<IActionResult> Get()
{
// Can also call RunCheckAsync to run a single Heatlh Check or RunGroupAsync to run a group of Health Checks
CompositeHealthCheckResult healthCheckResult = await _healthCheckService.CheckHealthAsync();
bool somethingIsWrong = healthCheckResult.CheckStatus != CheckStatus.Healthy;
if (somethingIsWrong)
{
// healthCheckResult has a .Description property, but that shows the description of all health checks.
// Including the successful ones, so let's filter those out
var failedHealthCheckDescriptions = healthCheckResult.Results.Where(r => r.Value.CheckStatus != CheckStatus.Healthy)
.Select(r => r.Value.Description)
.ToList();
// return a 500 with JSON containing the Results of the Health Check
return new JsonResult(new {Errors = failedHealthCheckDescriptions}){ StatusCode = StatusCodes.Status500InternalServerError };
}
return Ok();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment