项目
版本

单层解决方案:健康检查配置

健康检查是一项允许应用程序监控其自身状态并诊断潜在问题的功能。单层解决方案模板内置了预配置的健康检查系统。

在单层解决方案模板中,以下情况会应用健康检查配置:

  • 当选择 MVC 作为 Web 应用类型时。
  • 当选择 Blazor Server 作为 Web 应用类型时。
  • 当选择 Angular 作为 Web 应用类型时(在后端配置)。
  • 当选择 No UI 作为 Web 应用类型时(在后端配置)。

HealthChecksBuilderExtensions.cs 中的配置

健康检查在 HealthChecksBuilderExtensions 类中进行配置。该类扩展了 IServiceCollection 以注册健康检查服务并配置健康检查 UI 端点。

默认配置

默认设置如下:

using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;

namespace MyCompanyName.MyProjectName.HealthChecks;

public static class HealthChecksBuilderExtensions
{
    public static void AddMyProjectNameHealthChecks(this IServiceCollection services)
    {
        // 在此添加您的健康检查
        var healthChecksBuilder = services.AddHealthChecks();
        healthChecksBuilder.AddCheck<MyProjectNameDatabaseCheck>("MyProjectName DbContext 检查", tags: new string[] { "database" });

        // 从配置中读取健康检查 URL
        var configuration = services.GetConfiguration();
        var healthCheckUrl = configuration["App:HealthCheckUrl"] ?? "/health-status";
        
        services.ConfigureHealthCheckEndpoint(healthCheckUrl);

        // 配置 HealthChecks UI
        var healthChecksUiBuilder = services.AddHealthChecksUI(settings =>
        {
            settings.AddHealthCheckEndpoint("MyProjectName 健康状态", healthCheckUrl);
        });

        // 设置 HealthCheck UI 存储
        healthChecksUiBuilder.AddInMemoryStorage();

        services.MapHealthChecksUiEndpoints(options =>
        {
            options.UIPath = "/health-ui";
            options.ApiPath = "/health-api";
        });
    }

    private static IServiceCollection ConfigureHealthCheckEndpoint(this IServiceCollection services, string path)
    {
       ....
    }

    private static IServiceCollection MapHealthChecksUiEndpoints(this IServiceCollection services, Action<global::HealthChecks.UI.Configuration.Options>? setupOption = null)
    {
       ....
    }
}

数据库健康检查实现

MyProjectNameDatabaseCheck 类是一个自定义的健康检查实现,它使用 IIdentityRoleRepository 来验证数据库连接性。

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;

namespace MyCompanyName.MyProjectName.HealthChecks;

public class MyProjectNameDatabaseCheck : IHealthCheck, ITransientDependency
{
    protected readonly IIdentityRoleRepository IdentityRoleRepository;

    public MyProjectNameDatabaseCheck(IIdentityRoleRepository identityRoleRepository)
    {
        IdentityRoleRepository = identityRoleRepository;
    }

    public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
    {
        try
        {
            await IdentityRoleRepository.GetListAsync(sorting: nameof(IdentityRole.Id), maxResultCount: 1, cancellationToken: cancellationToken);
            return HealthCheckResult.Healthy($"可以连接到数据库并获取记录。");
        }
        catch (Exception e)
        {
            return HealthCheckResult.Unhealthy($"尝试获取数据库记录时出错。", e);
        }
    }
}

在本文档中