feat: 增加GC检查器
This commit is contained in:
parent
dd8aaa633c
commit
c0261e1ce3
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
{
|
||||
builder.AddCheck<DBHealthCheck>("db");
|
||||
builder.AddCheck<FileHealCheck>("file");
|
||||
builder.AddCheck<GCHealthCheck>("gc");
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue