首页 > 解决方案 > 如何使用 Asp.Net-Core HealthChecks 设置 Application Insights

问题描述

Asp.Net Core 发布了 2.2 版,并附带了 HealthChecks 功能。(阅读更多)。它的功能之一是将运行状况检查结果推送到 Azure Application Insights。但我还没有找到任何方法可以在 Azure 门户中查看这些结果。要发送结果,我使用以下扩展:

        services.AddHealthChecks()
            .AddSqlServer("...")
            .AddApplicationInsightsPublisher();

有没有办法在 Application Insights 中查看这些运行状况检查报告?

编辑 1:我以官方github docs为例。

编辑 2:如果我转到 Azure 门户查询分析,我会看到以下结果:

查询requests

在此处输入图像描述

查询customEvents

在此处输入图像描述

这里:GET /health是我的 healthCheck 端点。通过查询requests日志,我可以查看健康检查是否失败,但我想查看有关每个健康检查的更多详细信息,而且我认为我不需要对此进行任何扩展,所以我不明白AddApplicationInsightsPublisher()实际做了什么。

标签: azure-application-insightsasp.net-core-2.2

解决方案


目前发布者(HealthCheckPublisherHostedService)的注册存在问题,该问题将为 aspnet core 3 修复。目前的解决方法是手动正确注册类:

services.AddHealthChecks()
        .AddApplicationInsightsPublisher();

// This is a hack to fix an issue with the AddApplicationInsightsPublisher() call above
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHostedService), typeof(HealthCheckPublisherOptions).Assembly.GetType("Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckPublisherHostedService")));

请参阅:https ://github.com/aspnet/Extensions/issues/639


推荐阅读