BootstrapAdmin/Bootstrap.Admin/HealthChecks/GCHealthCheck.cs

35 lines
1.2 KiB
C#
Raw Normal View History

2019-08-13 14:54:26 +08:00
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>
2019-08-13 15:55:26 +08:00
/// 异步检查方法
2019-08-13 14:54:26 +08:00
/// </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) },
};
return Task.FromResult(HealthCheckResult.Healthy("OK", data));
2019-08-13 14:54:26 +08:00
}
}
}