Last active
May 22, 2017 18:00
-
-
Save scottsauber/948c96099e3fd50dc9725b7256b0ed27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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