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
37
38
39
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
 
namespace Bootstrap.Security.Mvc.HealthChecks
{
    /// <summary>
    /// 环境变量健康检查类
    /// </summary>
    internal class EnvironmentCheck : IHealthCheck
    {
        /// <summary>
        /// 异步检查方法
        /// </summary>
        /// <param name="context"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var data = new Dictionary<string, object>()
            {
                { "MachineName", Environment.MachineName }
            };
            var source = Environment.GetEnvironmentVariables();
            foreach (var key in source.Keys)
            {
                if (key != null)
                {
                    var k = key as string;
                    var val = source[key];
                    if (k != null && val != null) data.Add(k, val);
                }
            }
            return Task.FromResult(HealthCheckResult.Healthy("OK", data));
        }
    }
}