feat: 增加GC检查器

This commit is contained in:
Argo Zhang 2019-08-13 14:54:26 +08:00
parent dd8aaa633c
commit c0261e1ce3
No known key found for this signature in database
GPG Key ID: 152E398953DDF19F
2 changed files with 39 additions and 0 deletions

View File

@ -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
{
/// <summary>
/// 内存状态检查其
/// </summary>
public class GCHealthCheck : IHealthCheck
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
// Include GC information in the reported diagnostics.
var allocated = GC.GetTotalMemory(forceFullCollection: false);
var data = new Dictionary<string, object>()
{
{ "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));
}
}
}

View File

@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
{
builder.AddCheck<DBHealthCheck>("db");
builder.AddCheck<FileHealCheck>("file");
builder.AddCheck<GCHealthCheck>("gc");
return builder;
}
}