From 09729557e769ed4abce0c1f60f491a8ad5635dbc Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 29 Jun 2019 17:47:15 +1200 Subject: [PATCH] Adding response writing to provide useful output to the health check caller --- HealthChecks.SomeModelService/Startup.cs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/HealthChecks.SomeModelService/Startup.cs b/HealthChecks.SomeModelService/Startup.cs index 17be3f3..99baf34 100644 --- a/HealthChecks.SomeModelService/Startup.cs +++ b/HealthChecks.SomeModelService/Startup.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading.Tasks; using Autofac; using HealthChecks.Configuration; @@ -103,7 +103,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplica app.UseHealthChecks("/health", new HealthCheckOptions() { - Predicate = _ => true + ResponseWriter = WriteHealthCheckResponse }); //Use Identity and Access Control @@ -113,6 +113,23 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplica app.UseMvc(); } + private static Task WriteHealthCheckResponse(HttpContext httpContext, + HealthReport result) + { + httpContext.Response.ContentType = "application/json"; + + var json = new JObject( + new JProperty("status", result.Status.ToString()), + new JProperty("results", new JObject(result.Entries.Select(pair => + new JProperty(pair.Key, new JObject( + new JProperty("status", pair.Value.Status.ToString()), + new JProperty("description", pair.Value.Description), + new JProperty("data", new JObject(pair.Value.Data.Select( + p => new JProperty(p.Key, p.Value)))))))))); + return httpContext.Response.WriteAsync( + json.ToString(Formatting.Indented)); + } + /// /// Used to configure your container as needed. ///