using Microsoft.Extensions.Diagnostics.HealthChecks; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Bootstrap.Admin.HealthChecks { /// /// 内存状态检查其 /// public class GCHealthCheck : IHealthCheck { /// /// 异步检查方法 /// /// /// /// public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { // Include GC information in the reported diagnostics. var allocated = GC.GetTotalMemory(forceFullCollection: false); var data = new Dictionary() { { "AllocatedMBytes", allocated / 1024 / 1024 }, { "Gen0Collections", GC.CollectionCount(0) }, { "Gen1Collections", GC.CollectionCount(1) }, { "Gen2Collections", GC.CollectionCount(2) }, }; var status = (allocated < 100000) ? HealthStatus.Healthy : HealthStatus.Unhealthy; return Task.FromResult(new HealthCheckResult(status, data: data)); } } }