コード例 #1
0
        public static Task WriteHealthCheckUiResponse(
            HttpContext httpContext,
            HealthReport report,
            Action <JsonSerializerSettings> jsonConfigurator)
        {
            var text = "{}";

            if (report == null)
            {
                return(httpContext.Response.WriteAsync(text));
            }
            var settings = new JsonSerializerSettings()
            {
                ContractResolver      = new CamelCasePropertyNamesContractResolver(),
                NullValueHandling     = NullValueHandling.Ignore,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            };

            jsonConfigurator?.Invoke(settings);
            settings.Converters.Add(new StringEnumConverter());
            httpContext.Response.ContentType = DEFAULT_CONTENT_TYPE;
            try
            {
                var uiReport = UIHealthReport.CreateFrom(report);
                text = JsonConvert.SerializeObject(uiReport, settings);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return(httpContext.Response.WriteAsync(text));
        }
        private async Task <UIHealthReport> GetHealthReport(HealthCheckConfiguration configuration)
        {
            var(uri, name) = configuration;

            try
            {
                var response = await _httpClient.GetAsync(uri);

                return(await response.As <UIHealthReport>());
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"GetHealthReport threw an exception when trying to get report from {uri} configured with name {name}.");

                return(UIHealthReport.CreateFrom(exception));
            }
        }
コード例 #3
0
        public IHttpActionResult Get()
        {
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();

            var entries = new Dictionary <string, HealthReportEntry>();

            entries.Add("DI Controllers", HealthCheckHelper.TestaControllersInjection("HealthCheck.API", IoC.Container));

            stopwatch.Stop();

            var response = UIHealthReport.CreateFrom(
                new HealthReport(entries, TimeSpan.FromMilliseconds(stopwatch.ElapsedMilliseconds)));

            if (response.Status == UIHealthStatus.Healthy)
            {
                return(Ok(response));
            }
            else
            {
                return(Content((System.Net.HttpStatusCode) 418, response));
            }
        }