Exemple #1
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            NetworkCheckOptions opts = _options.Get(context.Registration.Name);

            bool networkIsAvailable = false;

            IPAddress ipAddress = IPAddress.Parse(opts.InternetHost);

            Stopwatch discoverySw = new Stopwatch();

            discoverySw.Start();

            try
            {
                networkIsAvailable = await ipAddress.CheckPingAsync();
            }
            catch (Exception e)
            {
                _log.LogError("Error checking health for Network: {Exception}", e);
            }

            discoverySw.Stop();

            int networkLatencyValue = (int)discoverySw.ElapsedMilliseconds;

            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { "InternetConnectionLatency", networkLatencyValue }
            };

            HealthStatus status = networkLatencyValue < opts.MaxLatencyThreshold && networkIsAvailable ? HealthStatus.Healthy : HealthStatus.Unhealthy;

            return(new HealthCheckResult(status, $"Reports degraded status if no Internet connection available or latency >= {opts.MaxLatencyThreshold} milliseconds.", null, data));
        }
Exemple #2
0
    public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        NetworkCheckOptions opts = _options.Get(context.Registration.Name);

        bool networkIsAvailable = false;

        IPAddress ipAddress = IPAddress.Parse(opts.InternetHost);

        DateTime startDt = DateTime.UtcNow;

        try
        {
            networkIsAvailable = await ipAddress.CheckPingAsync();
        }
        catch (Exception ex)
        {
            _log.LogError(ex, "Error checking health for Network");
        }

        DateTime stopDt         = DateTime.UtcNow;
        TimeSpan processingTime = stopDt - startDt;

        int networkCheckTime = (int)processingTime.TotalMilliseconds;

        Dictionary <string, object> data = new Dictionary <string, object>
        {
            { "InternetConnectionLatency", networkCheckTime }
        };

        HealthStatus status = networkCheckTime < opts.MaxLatencyThreshold && networkIsAvailable ? HealthStatus.Healthy : HealthStatus.Unhealthy;

        return(new HealthCheckResult(status, $"Reports degraded status if no Internet connection available or latency >= {opts.MaxLatencyThreshold} milliseconds.", null, data));
    }