예제 #1
0
        public static HealthCheckBuilder AddUrlChecks(this HealthCheckBuilder builder, IEnumerable <string> urlItems, string group)
        {
            var urls = urlItems.ToList();

            builder.AddCheck($"UrlChecks ({group})", async() =>
            {
                var successfulChecks = 0;
                var description      = new StringBuilder();
                var httpClient       = new HttpClient();

                foreach (var url in urlItems)
                {
                    try
                    {
                        httpClient.DefaultRequestHeaders.Add("cache-control", "no-cache");
                        var response = await httpClient.GetAsync(url);

                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            successfulChecks++;
                            description.Append($"UrlCheck SUCCESS ({url}) ");
                        }
                        else
                        {
                            description.Append($"UrlCheck FAILED ({url}) ");
                        }
                    }
                    catch
                    {
                        description.Append($"UrlCheck FAILED ({url}) ");
                    }
                }

                if (successfulChecks == urls.Count)
                {
                    return(HealthCheckResult.Healthy(description.ToString()));
                }
                else if (successfulChecks > 0)
                {
                    return(HealthCheckResult.Warning(description.ToString()));
                }

                return(HealthCheckResult.Unhealthy(description.ToString()));
            });
            return(builder);
        }
예제 #2
0
            public async void GroupsIntegrationTest()
            {
                _builder.WithPartialSuccessStatus(CheckStatus.Warning)
                .AddCheck("c1", () => HealthCheckResult.Healthy("Healthy c1"))
                .AddHealthCheckGroup("g1",
                                     group => group.AddCheck("gc1", () => HealthCheckResult.Unhealthy("Unhealthy gc1"))
                                     .AddCheck("gc2", () => HealthCheckResult.Warning("Warning gc2")));
                var service = new HealthCheckService(_builder, _serviceProvider, _serviceProvider.ScopeFactory);

                var result = await service.CheckHealthAsync();

                Assert.Equal("Healthy (Healthy c1)", ToDescriptiveString(result.Results["c1"]));
                var groupResult = Assert.IsType <CompositeHealthCheckResult>(result.Results["Group(g1)"]);

                Assert.Equal(CheckStatus.Unhealthy, groupResult.CheckStatus);
                Assert.Collection(groupResult.Results.OrderBy(kvp => kvp.Key).Select(ToDescriptiveString),
                                  item => Assert.Equal("'gc1' = 'Unhealthy (Unhealthy gc1)'", item),
                                  item => Assert.Equal("'gc2' = 'Warning (Warning gc2)'", item)
                                  );
            }