• dotnet healthcheck


    配置healthchecks ui ,必须在appsetting.json中指定/health的地址

    https://localhost:44398/healthchecks-ui

      "HealthChecks-UI": {
        "HealthChecks": [
          {
            "Name": "Ordering HTTP Check",
            "Uri": "https://localhost:44398/health"
          }
        ]
      }

    https://localhost:44398/healthchecks-ui#/healthchecks

                context.Services.AddHealthChecks()
                    .AddSqlServer(configuration["ConnectionStrings:Default"])
                    .AddMongoDb(configuration["ConnectionStrings:TestMongoDb"])
                    .AddCheck<MemoryHealthCheck>("MemoryHealthCheck")
                    .AddCheck<ApiHealthCheck>("ApiHealthCheck")
                    .AddCheck<WindowsServicesHealthCheck>("RpcSs");
    
                context.Services.AddHealthChecksUI();//.AddInMemoryStorage();
                //    setupSettings: setup =>
                //{
                //    setup.SetEvaluationTimeInSeconds(60); //Configures the UI to poll for healthchecks updates every 5 seconds
                //});
    

      

    app.UseHealthChecks("/health", new HealthCheckOptions()
                {
                    Predicate = _ => true,
                    ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
                });
                app.UseHealthChecksUI()

    可以更改路径

    app.UseHealthChecks("/hc", new HealthCheckOptions()
            {
                Predicate = _ => true,
                ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
            })
            .UseHealthChecksUI(setup => 
            {
                setup.ApiPath = "/hc";
                setup.UIPath = "/hc-ui";
            });

    ApiHealthCheck

        public class ApiHealthCheck : IHealthCheck
        {
            private readonly IHttpClientFactory _httpClientFactory;
    
    
            public ApiHealthCheck(IHttpClientFactory httpClientFactory)
            {
                _httpClientFactory = httpClientFactory;
            }
            public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context,
                CancellationToken cancellationToken = default)
            {
                using (var httpClient = _httpClientFactory.CreateClient())
                {
                    var response = await httpClient.GetAsync("https://localhost:44398/api/app/item/items");
                    if (response.IsSuccessStatusCode)
                    {
                        return HealthCheckResult.Healthy($"API is running.");
                    }
    
    
                    return HealthCheckResult.Unhealthy("API is not running");
                }
            }
        }

    MemoryHealthCheck

     

        #region snippet1
        public class MemoryHealthCheck : IHealthCheck
        {
            private readonly IOptionsMonitor<MemoryCheckOptions> _options;
    
            public MemoryHealthCheck(IOptionsMonitor<MemoryCheckOptions> options)
            {
                _options = options;
            }
    
            public string Name => "memory_check";
    
            public Task<HealthCheckResult> CheckHealthAsync(
                HealthCheckContext context,
                CancellationToken cancellationToken = default(CancellationToken))
            {
                var options = _options.Get(context.Registration.Name);
    
                // Include GC information in the reported diagnostics.
                var allocated = GC.GetTotalMemory(forceFullCollection: false);
                var data = new Dictionary<string, object>()
                {
                    { "AllocatedBytes", allocated },
                    { "Gen0Collections", GC.CollectionCount(0) },
                    { "Gen1Collections", GC.CollectionCount(1) },
                    { "Gen2Collections", GC.CollectionCount(2) },
                };
    
                var status = (allocated < options.Threshold) ?
                    HealthStatus.Healthy : HealthStatus.Unhealthy;
    
                return Task.FromResult(new HealthCheckResult(
                    status,
                    description: "Reports degraded status if allocated bytes " +
                        $">= {options.Threshold} bytes.",
                    exception: null,
                    data: data));
            }
        }
    
    #endregion
    
        #region snippet2
        public static class GCInfoHealthCheckBuilderExtensions
        {
            public static IHealthChecksBuilder AddMemoryHealthCheck(
                this IHealthChecksBuilder builder,
                string name,
                HealthStatus? failureStatus = null,
                IEnumerable<string> tags = null,
                long? thresholdInBytes = null)
            {
                // Register a check of type GCInfo.
                builder.AddCheck<MemoryHealthCheck>(
                    name, failureStatus ?? HealthStatus.Degraded, tags);
    
                // Configure named options to pass the threshold into the check.
                if (thresholdInBytes.HasValue)
                {
                    builder.Services.Configure<MemoryCheckOptions>(name, options =>
                    {
                        options.Threshold = thresholdInBytes.Value;
                    });
                }
    
                return builder;
            }
        }
        #endregion
    
        #region snippet3
        public class MemoryCheckOptions
        {
            // Failure threshold (in bytes)
            public long Threshold { get; set; } = 1024L * 1024L * 1024L;
        }
        #endregion
    

      

    WindowsServicesHealthCheck

    public class WindowsServicesHealthCheck : IHealthCheck
        {
            private readonly string _serviceName;
            private readonly IOptionsMonitor<WindowsServicesCheckOptions> _options;
      
            public WindowsServicesHealthCheck(IOptionsMonitor<WindowsServicesCheckOptions> options)
            {
                _options = options;
            }
    
    
            public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
            {
                var status = GetWindowsServiceStatus(context.Registration.Name);
    
                return Task.FromResult(new HealthCheckResult(
                    (status == ServiceControllerStatus.Running)?HealthStatus.Healthy : HealthStatus.Unhealthy,
                    description: "",
                    exception: null,
                    data: null));
            }
    
            public static ServiceControllerStatus GetWindowsServiceStatus(String SERVICENAME)
            {
    
                ServiceController sc = new ServiceController(SERVICENAME);
                return sc.Status;
    
                //switch (sc.Status)
                //{
                //    case ServiceControllerStatus.Running:
                //        return "Running";
                //    case ServiceControllerStatus.Stopped:
                //        return "Stopped";
                //    case ServiceControllerStatus.Paused:
                //        return "Paused";
                //    case ServiceControllerStatus.StopPending:
                //        return "Stopping";
                //    case ServiceControllerStatus.StartPending:
                //        return "Starting";
                //    default:
                //        return "Status Changing";
                //}
            }
            public class WindowsServicesCheckOptions
            {
                public string Name { get; set; } 
            }
    
        }
  • 相关阅读:
    Django测试开发-4-django3.0.3报错:/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
    Django测试开发-3-新建一个Django工程
    Django测试开发-2-创建虚拟环境
    Django测试开发-1-MVC/MVT的概念
    sqlserver中复合索引和include索引到底有多大区别?
    PLSQL开发笔记和小结(转载)
    PL/sql语法单元
    jquery 获取下拉框值与select text
    jquery 事件
    jquery dom操作
  • 原文地址:https://www.cnblogs.com/sui84/p/14938785.html
Copyright © 2020-2023  润新知