Example #1
0
        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}");
                }
            }
        }
        public HealthMonitorResult RestartServices(ServiceController[] services)
        {
            HealthMonitorResult resultToStop = StopServices(services);

            resultToStop.MessageBuilder.Insert(0, Environment.NewLine);
            resultToStop.MessageBuilder.Insert(0, "The Result of Stopping Services:");
            if (resultToStop.IsSerious)
            {
                return(resultToStop);
            }
            HealthMonitorResult resultToStart = StartServices(services);

            resultToStart.MessageBuilder.Insert(0, Environment.NewLine);
            resultToStart.MessageBuilder.Insert(0, "The Result of Starting Services:");
            if (resultToStart.IsSerious)
            {
                return(resultToStart);
            }
            HealthMonitorResult result = new HealthMonitorResult(Name, HealthType, ResultStatus.Warning, resultToStop.ToString());

            result.MessageBuilder.Append(Environment.NewLine);
            result.MessageBuilder.Append(resultToStart.ToString());
            return(result);
        }