private BaseIndividualHealthCheckResult IndividualHealthCheck(IHealthCheck healthCheck) { var healthCheckName = healthCheck.GetType().Name; var sw = Stopwatch.StartNew(); var task = Task.Run <BaseIndividualHealthCheckResult>( () => { try { healthCheck.Check(); } catch (Exception ex) { logger.Error($"Health check { healthCheckName} ({ healthCheck.Description}) error.", ex); return(new FailedIndividualHealthCheckResult( healthCheckName, healthCheck.Description, ex.Message, healthCheck.CriticalMarking, sw.Elapsed )); } finally { sw.Stop(); } return(new SuccessfulIndividualHealthCheckResult( healthCheckName, healthCheck.Description, healthCheck.CriticalMarking, sw.Elapsed )); } ); var isTaskCompletedInTime = Task.WaitAll(new Task[] { task }, timeout); if (!isTaskCompletedInTime) { logger.Error($"Health check {healthCheckName} ({healthCheck.Description}) timed out."); return(new FailedIndividualHealthCheckResult( healthCheckName, healthCheck.Description, "Health check timed out.", healthCheck.CriticalMarking, sw.Elapsed )); } return(task.Result); }
private IndividualHealthCheckResult CheckIndividualInternal(IHealthCheck healthCheck, string healthCheckName, Stopwatch sw) { try { healthCheck.Check(); return(new SuccessfulIndividualHealthCheckResult(healthCheckName, healthCheck.Description, sw.Elapsed)); } catch (Exception ex) { var message = string.Format(CultureInfo.CurrentUICulture, "Health check {0} failed: {1}", healthCheckName, ex.Message); _logException(ex, message); return(new FailedIndividualHealthCheckResult(healthCheckName, healthCheck.Description, ex.Message, sw.Elapsed)); } }
public async Task <HealthCheckStatus> Check() { var now = DateTimeOffset.Now; if (_lastStatus == null || (_cacheFailures && _lastStatus.HasFailed) || _lastChecked + _cacheTime < now) { _lastStatus = await _healthCheck.Check().ConfigureAwait(false); _lastChecked = now; return(_lastStatus); } return(_lastStatus); }
private IndividualHealthCheckResult CheckIndividualInternal(IHealthCheck healthCheck, string healthCheckName, Stopwatch sw) { try { healthCheck.Check(); return new SuccessfulIndividualHealthCheckResult(healthCheckName, healthCheck.Description, sw.Elapsed); } catch (Exception ex) { var message = "Health check {0} failed: {1}".FormatWith(healthCheckName, ex.Message); _logException(ex, message); return new FailedIndividualHealthCheckResult(healthCheckName, healthCheck.Description, ex.Message, sw.Elapsed); } }
private static IList <HealthCheckResult> TryCheck(IHealthCheck check) { try { return(check.Check()); } catch (Exception ex) { return(GetExceptionHealthCheckResult(check, ex)); } }