diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs new file mode 100644 index 00000000..621e1b7a --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -0,0 +1,27 @@ +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace Bootstrap.Admin.HealthChecks +{ + /// + /// 数据库检查 + /// + public class DBHealthCheck : IHealthCheck + { + /// + /// 异步检查方法 + /// + /// + /// + /// + 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)); + } + } +} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs new file mode 100644 index 00000000..82032a2d --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/HealthChecksAppBuilderExtensions.cs @@ -0,0 +1,19 @@ +namespace Microsoft.AspNetCore.Builder +{ + /// + /// BootstrapAdmin 健康检查扩展类 + /// + public static class HealthChecksAppBuilderExtensions + { + /// + /// + /// + /// + /// + public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app) + { + app.UseHealthChecks("Healths"); + return app; + } + } +} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs new file mode 100644 index 00000000..260a38cb --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -0,0 +1,19 @@ +namespace Microsoft.Extensions.DependencyInjection +{ + /// + /// 健康检查扩展类 + /// + public static class HealthChecksBuilderExtensions + { + /// + /// 添加 BootstrapAdmin 健康检查 + /// + /// + /// + public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) + { + builder.AddCheck("db"); + return builder; + } + } +} diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 5715b9eb..b880f414 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -64,6 +64,7 @@ namespace Bootstrap.Admin services.AddSwagger(); services.AddButtonAuthorization(); services.AddDemoTask(); + services.AddHealthChecks().AddBootstrapAdminHealthChecks(); services.AddMvc(options => { options.Filters.Add(); @@ -107,6 +108,7 @@ namespace Bootstrap.Admin app.UseHttpsRedirection(); app.UseResponseCompression(); app.UseStaticFiles(); + app.UseBootstrapHealthChecks(); app.UseBootstrapAdminAuthentication(RoleHelper.RetrievesByUserName, RoleHelper.RetrievesByUrl, AppHelper.RetrievesByUserName); app.UseOnlineUsers(callback: TraceHelper.Save); app.UseCacheManager();