diff --git a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 736f56f5..4bf090ea 100644 --- a/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -9,7 +9,7 @@ using Task = System.Threading.Tasks.Task; namespace Bootstrap.Admin.HealthChecks { /// - /// 数据库检查 + /// 数据库检查类 /// public class DBHealthCheck : IHealthCheck { diff --git a/Bootstrap.Admin/HealthChecks/FileHealCheck.cs b/Bootstrap.Admin/HealthChecks/FileHealCheck.cs new file mode 100644 index 00000000..ef5ed450 --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/FileHealCheck.cs @@ -0,0 +1,44 @@ +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace Bootstrap.Admin.HealthChecks +{ + /// + /// 文件健康检查类 + /// + public class FileHealCheck : IHealthCheck + { + private readonly IHostingEnvironment _env; + /// + /// 构造函数 + /// + /// + public FileHealCheck(IHostingEnvironment env) + { + _env = env; + } + + /// + /// 异步检查方法 + /// + /// + /// + /// + public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) + { + var file = _env.IsDevelopment() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Longbow.lic") : Path.Combine(_env.ContentRootPath, "Longbow.lic"); + var data = new Dictionary(); + data.Add("ContentRootPath", _env.ContentRootPath); + data.Add("WebRootPath", _env.WebRootPath); + data.Add("ApplicationName", _env.ApplicationName); + data.Add("EnvironmentName", _env.EnvironmentName); + data.Add("CheckFile", file); + return Task.FromResult(File.Exists(file) ? HealthCheckResult.Healthy("Ok", data) : HealthCheckResult.Unhealthy($"Missing file {file}", null, data)); + } + } +} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs index d5352528..f01887f9 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -15,6 +15,7 @@ namespace Microsoft.Extensions.DependencyInjection public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder) { builder.AddCheck("db"); + builder.AddCheck("file"); return builder; } }