zhaolei
7 days ago 921de2254ff5712a44ed8575ee8efe34252f6603
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
 
namespace Bootstrap.Security.Mvc.HealthChecks
{
    /// <summary>
    /// 内存检查器
    /// </summary>
    internal class MemoryHealthCheck : IHealthCheck
    {
        /// <summary>
        /// 异步检查方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var process = Process.GetCurrentProcess();
            var data = new Dictionary<string, object>()
            {
                { "Id", process.Id },
                { "WorkingSet", process.WorkingSet64  },
                { "PrivateMemory", process.PrivateMemorySize64 },
                { "VirtualMemory", process.VirtualMemorySize64 },
            };
 
            return Task.FromResult(HealthCheckResult.Healthy("Ok", data));
        }
    }
}