3

我已经在 Asp.net 核心应用程序中设置了应用程序洞察力。我所有的 web api 请求都在应用洞察中进行跟踪,如果我有任何失败,我可以简单地在Failures部分中找到它们。

应用洞察

但是,我还运行了 Hangfire 后台作业,如果它们失败,我无法在应用洞察力中找到它们。我也有警报规则Whenever the total http server errors is greater than or equal to 1 count,我不确定在这种情况下是否会出现hangfire 5xx 错误。

那么有什么方法可以跟踪 Hangfire 作业失败并获得有关它们的通知?

4

2 回答 2

3

Hangfire 在后台处理大多数异常,因此默认情况下 App Insights 不会选择它们。您还必须使用 App Insights 进行大量配置。

我为 Hangfire 编写了一个 JobFilter,它允许您连接 App Insights,这应该足以让您继续前进: https ://github.com/maitlandmarshall/MIFCore/blob/master/MIFCore.Hangfire/Analytics/AppInsightsEventsFilter.cs

对于 App Insights 配置: https ://github.com/maitlandmarshall/MIFCore/blob/master/MIFCore.Hangfire/Analytics/TelemetryConfigurationFactory.cs

将上述链接中的所有内容放在一起:

var appInsights = this.rootScope.ResolveOptional<AppInsightsConfig>();
var childScope = ServiceScope = this.rootScope.BeginLifetimeScope("HangfireServiceScope");
var activator = new AutofacLifecycleJobActivator(childScope);
var options = new BackgroundJobServerOptions()
{
    Activator = activator,
    Queues = new[] { JobQueue.Alpha, JobQueue.Beta, JobQueue.Default, JobQueue.Low }
};

this.globalConfig
    .UseFilter(new BackgroundJobContext());

if (!string.IsNullOrEmpty(appInsights?.InstrumentationKey))
{
    var telemetryClient = new TelemetryClient(TelemetryConfigurationFactory.Create(appInsights));
    this.globalConfig.UseFilter(new AppInsightsEventsFilter(telemetryClient));
}

using (var server = new BackgroundJobServer(options))
{
    await server.WaitForShutdownAsync(stoppingToken);
}
于 2020-11-26T05:29:06.073 回答
0

创建了一个不错的nuget 包Hangfire.Extensions.ApplicationInsights

所以,安装包:

Install-Package Hangfire.Extensions.ApplicationInsights

并将该行添加到ConfigureService方法中:

services.AddHangfireApplicationInsights();

如果您的解决方案需要一些自定义细节,您可以从github 存储库调整代码。

于 2022-01-05T08:17:29.363 回答