Dei*_*kis 5 telemetry hangfire asp.net-core appinsights
我已经在 Asp.net core 应用程序中设置了应用程序见解。我的所有 Web API 请求都会在应用程序洞察中进行跟踪,如果我遇到任何失败,我可以简单地在Failures部分中找到它们。
但是,我还运行了 Hangfire 后台作业,如果它们失败,我无法在应用程序洞察中找到它们。另外,我有警报规则Whenever the total http server errors is greater than or equal to 1 count,我不确定在这种情况下是否会出现hangfire 5xx 错误。
那么有什么方法可以跟踪 Hangfire 作业失败并获得相关通知吗?
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);
}
Run Code Online (Sandbox Code Playgroud)
创建了一个不错的nuget 包Hangfire.Extensions.ApplicationInsights。
因此,安装该包:
Install-Package Hangfire.Extensions.ApplicationInsights
并将该行添加到ConfigureService方法中:
services.AddHangfireApplicationInsights();
如果您的解决方案需要一些自定义详细信息,您可以调整github 存储库中的代码。
| 归档时间: |
|
| 查看次数: |
2122 次 |
| 最近记录: |