From a191a9e383b380610e7c38b2affea14af5f78afd Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 13 Aug 2019 14:19:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89json=E6=A0=BC=E5=BC=8F=E8=BE=93=E5=87=BA=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/HealthChecks/DBHealthCheck.cs | 17 +++++++++------ .../HealthChecksAppBuilderExtensions.cs | 21 +++++++++++++++---- .../HealthChecksBuilderExtensions.cs | 6 ++++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 621e1b7a..736f56f5 100644 --- a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -1,7 +1,10 @@ -using Microsoft.Extensions.Diagnostics.HealthChecks; -using System.Collections.Generic; +using Bootstrap.DataAccess; +using Bootstrap.Security; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.Linq; using System.Threading; using System.Threading.Tasks; +using Task = System.Threading.Tasks.Task; namespace Bootstrap.Admin.HealthChecks { @@ -18,10 +21,12 @@ namespace Bootstrap.Admin.HealthChecks /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { - var data = new Dictionary(); - data.Add("Test1", "Test1"); - data.Add("Test2", "Test2"); - return Task.FromResult(HealthCheckResult.Healthy("Ok", data)); + using (var db = DbManager.Create()) + { + var connStr = db.ConnectionString; + var dicts = db.Fetch("Select * from Dicts"); + return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok")) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB")); + } } } } diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs index 82032a2d..3b07e2f5 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs @@ -1,4 +1,8 @@ -namespace Microsoft.AspNetCore.Builder +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; + +namespace Microsoft.AspNetCore.Builder { /// /// BootstrapAdmin 健康检查扩展类 @@ -6,13 +10,22 @@ public static class HealthChecksAppBuilderExtensions { /// - /// + /// 启用健康检查 /// /// + /// /// - public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app) + public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app, PathString path = default) { - app.UseHealthChecks("Healths"); + if (path == default) path = "/Healths"; + app.UseHealthChecks(path, new HealthCheckOptions() + { + ResponseWriter = (context, report) => + { + context.Response.ContentType = "application/json"; + return context.Response.WriteAsync(JsonConvert.SerializeObject(report)); + } + }); return app; } } diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs index 260a38cb..d5352528 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -1,4 +1,6 @@ -namespace Microsoft.Extensions.DependencyInjection +using Bootstrap.Admin.HealthChecks; + +namespace Microsoft.Extensions.DependencyInjection { /// /// 健康检查扩展类 @@ -12,7 +14,7 @@ /// public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) { - builder.AddCheck("db"); + builder.AddCheck("db"); return builder; } }