From c0261e1ce3211955cb4b5df3a46d6be8eded5e9d Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Tue, 13 Aug 2019 14:54:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0GC=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/HealthChecks/GCHealthCheck.cs | 38 +++++++++++++++++++ .../HealthChecksBuilderExtensions.cs | 1 + 2 files changed, 39 insertions(+) create mode 100644 Bootstrap.Admin/HealthChecks/GCHealthCheck.cs diff --git a/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs b/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs new file mode 100644 index 00000000..334755f2 --- /dev/null +++ b/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs @@ -0,0 +1,38 @@ +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)); + } + } +} diff --git a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs index f01887f9..5c0d0728 100644 --- a/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs +++ b/Bootstrap.Admin/HealthChecks/HealthChecksBuilderExtensions.cs @@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection { builder.AddCheck("db"); builder.AddCheck("file"); + builder.AddCheck("gc"); return builder; } }