protected override void OnStart(string[] args) { Log.Debug("OnStart"); if (HealthChecksSection == null || HealthChecksSection.HealthChecks == null || HealthChecksSection.HealthChecks.Count == 0) { Log.Error(SystemConstants.MISSING_HEALTH_CHECK_SECTION); } try { HealthChecks healthCheckConfigs = HealthChecksSection.GetConfig().HealthChecks; for (int ii = 0; ii < healthCheckConfigs.Count; ii++) { healthChecks.Add(healthCheckConfigs[ii]); } IKernel kernel = new BindKernelwithHealthChecks(new StandardKernel(new ServiceModule()), HealthChecksSection.HealthChecks).Bind(); healthCheckList = new HealthCheckList(healthChecks, kernel); healthCheckList.HealthCheckError += HealthCheckList_HealthCheckError; healthCheckList.SeriousResult += HealthCheckList_SeriousResult; healthCheckList.Start(); } catch (ScheduleProviderException ex) { Log.ErrorFormat($"Failed: {ex.ProviderName}"); Log.Error(ex); OnStop(); } finally { Log.Debug("Finished OnStart"); } }
static void Main(string[] args) { Log.Debug("Start Application"); if (HealthChecksSection == null || HealthChecksSection.HealthChecks == null || HealthChecksSection.HealthChecks.Count == 0) { Log.Error(SystemConstants.MISSING_HEALTH_CHECK_SECTION); return; } if (args.Length == 0) { Console.WriteLine("Enter one of HealthChecksSection Names:"); foreach (object key in HealthChecksSection.HealthChecks) { var healthCheck = key as HealthCheck; Console.WriteLine($"\"{healthCheck.Name}\""); } } else if (args.Length > 1) { Console.WriteLine($"Expected only 1 argument but found {args.Length}"); } else { string healthCheckName = args[0]; Log.DebugFormat($"Run The Health Check: \"{args[0]}\""); try { HealthChecksSection config = HealthChecksSection.GetConfig(); IEnumerable <BoundsLimit> bounds = config.HealthChecks[healthCheckName].HealthCheckParameters.ReturnAll() .Select(boundsLimit => new BoundsLimit(boundsLimit.Name, boundsLimit.Type, boundsLimit.Value)); IKernel kernel = new BindKernelwithHealthChecks(new StandardKernel(new ServiceModule()), HealthChecksSection.HealthChecks).Bind(); ApplicationHealthCheck app = kernel.Get <ApplicationHealthCheck>(healthCheckName); if (app == null) { throw new ArgumentException($"{healthCheckName} is not a valid Health Check Name"); } HealthMonitorResult healthMonitorResult = app.DoHealthCheck(bounds); Log.DebugFormat(healthMonitorResult.ToString()); if (healthMonitorResult.IsSerious) { //SendNotification(kernel, healthMonitorResult, config.HealthChecks[healthCheckName]); Console.WriteLine(healthMonitorResult); } } catch (Exception e) { Log.Error(e); } finally { Log.DebugFormat($"Finished health check: {healthCheckName}"); } } }