ASP.NET Core HealthChecks

ASP.NET Core HealthChecks

HealthChecks 有很多高级用法 —— 自定义检测健康状态、容器化、配合 Prometheus 等前端监控项目等等,这里只展示基本的使用方法

下文将展示 .NET 5 中 HealthChecks 的基本用法

环境

  • Visual Studio 2019
  • WIndows 10 2004
  • .NET 5

参考

步骤

ASP.NET Core 2.2 就已经新增了 HealthCheck,不用 Nuget 包安装

但是官方实现了一些基本的检测组件(检测网络、系统资源、数据库状态等等)

  1. 新建 ASP.NET Core 项目,安装 Nuget 组件
    Install-Package AspNetCore.HealthChecks.System
    Install-Package AspNetCore.HealthChecks.Network

    除了这两个组件外,还可以安装

    Install-Package AspNetCore.HealthChecks.SqlServer
    Install-Package AspNetCore.HealthChecks.MongoDb
    Install-Package AspNetCore.HealthChecks.Npgsql
    Install-Package AspNetCore.HealthChecks.Redis
    Install-Package AspNetCore.HealthChecks.AzureStorage
    Install-Package AspNetCore.HealthChecks.AzureServiceBus
    Install-Package AspNetCore.HealthChecks.MySql
    Install-Package AspNetCore.HealthChecks.DocumentDb
    Install-Package AspNetCore.HealthChecks.SqLite
    Install-Package AspNetCore.HealthChecks.Kafka
    Install-Package AspNetCore.HealthChecks.RabbitMQ
    Install-Package AspNetCore.HealthChecks.IdSvr
    Install-Package AspNetCore.HealthChecks.DynamoDB
    Install-Package AspNetCore.HealthChecks.Oracle
    Install-Package AspNetCore.HealthChecks.Uris

    以使用对应功能

  2. 在 Startup.cs 中注册 HealthChecks 服务
    public void ConfigureServices(IServiceCollection services) {
        //...
        services.AddHealthChecks()
            .AddDiskStorageHealthCheck(x => x.AddDrive("E", 1000_000_000L)) # 检测 E 盘是否有 1GB 空间
            .AddPrivateMemoryHealthCheck(1000_000_000L) # 最大私有内存不超过 1GB
            .AddVirtualMemorySizeHealthCheck(1000_000_000L) # 最大虚拟内存不超过 1GB
            .AddWorkingSetHealthCheck(1000_000_000L); # 最大工作内存不超过 1GB
    }

    在使用数据库检测时需要传入 ConnectionString,使用 Redis 检测则需要 Redis 服务器链接

  3. 启用 HealthChecks 并配置路由
    using Microsoft.AspNetCore.Diagnostics.HealthChecks;
    using Microsoft.AspNetCore.Http;
    using Newtonsoft.Json; # 第三方 Json 组件
    using Microsoft.Extensions.Diagnostics.HealthChecks;
    using System.Net.Mime;
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        //...
    
        app.UseHealthChecks("/health", new HealthCheckOptions {
            ResponseWriter = async (context, report) => {
                var result = JsonConvert.SerializeObject(
                    new {
                        status = report.Status.ToString(),
                        errors = report.Entries.Select(e => new { key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status) })
                    });
                context.Response.ContentType = MediaTypeNames.Application.Json;
                await context.Response.WriteAsync(result);
            }
        });
    
        //...
    }

自定义检测需要实现 IHealthCheck 接口,并在服务注册方法中添加

services.AddHealthChecks()
  .AddPrivateMemoryHealthCheck(“自定义检测名称”)

效果

ASP.NET Core HealthChecks

ASP.NET Core HealthChecks  ASP.NET Core HealthChecks

发表评论

邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据