zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
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
35
36
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>
    /// NETCore 运行时检查
    /// </summary>
    public class NetCoreRuntimeCheck : IHealthCheck
    {
        /// <summary>
        /// 异步检查方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var psi = new ProcessStartInfo("dotnet", "--info");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            var p = Process.Start(psi);
            p.WaitForExit(5000);
 
            var result = await p.StandardOutput.ReadToEndAsync();
            var data = new Dictionary<string, object>()
            {
                { "dotnet --info", result }
            };
            return await Task.FromResult(HealthCheckResult.Healthy("OK", data));
        }
    }
}