using Bootstrap.DataAccess; using Bootstrap.Security; using Microsoft.Extensions.Diagnostics.HealthChecks; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Task = System.Threading.Tasks.Task; namespace Bootstrap.Admin.HealthChecks { /// /// 数据库检查类 /// public class DBHealthCheck : IHealthCheck { /// /// 异步检查方法 /// /// /// /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { using (var db = DbManager.Create()) { var connStr = db.ConnectionString; var dicts = db.Fetch("Select * from Dicts"); var data = new Dictionary() { { "ConnectionString", connStr }, { "DbType", db.Provider.GetType().Name }, { "Dicts", dicts.Count } }; return dicts.Any() ? Task.FromResult(HealthCheckResult.Healthy("Ok", data)) : Task.FromResult(HealthCheckResult.Degraded("No init data in DB")); } } } }