如何将异常链接到 Azure 上的 Application Insights 中的请求?

How to link exceptions to requests in Application Insights on Azure?(如何将异常链接到 Azure 上的 Application Insights 中的请求?)
本文介绍了如何将异常链接到 Azure 上的 Application Insights 中的请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在 Azure 上使用 Owin 提供 REST 服务,并且必须直接向 Application Insights 报告.我们想要记录异常和请求.现在我们有这个:

We are using Owin on Azure for a REST service, and have to report to Application Insights directly. We want to log exceptions and requests. Right now we have this:

using AppFunc = Func<IDictionary<string, object>, Task>;
public class InsightsReportMiddleware
{

    readonly AppFunc next;
    readonly TelemetryClient telemetryClient;

    public InsightsReportMiddleware(AppFunc next, TelemetryClient telemetryClient)
    {
        if (next == null)
        {
            throw new ArgumentNullException("next");
        }

        this.telemetryClient = telemetryClient;
        this.next = next;
    }

    public async Task Invoke(IDictionary<string, object> environment)
    {
        var sw = new Stopwatch();
        sw.Start();

        await next(environment);
        sw.Stop();

        var ctx = new OwinContext(environment);
        var rt = new RequestTelemetry(
            name: ctx.Request.Path.ToString(),
            timestamp: DateTimeOffset.Now,
            duration: sw.Elapsed,
            responseCode: ctx.Response.StatusCode.ToString(),
            success: 200 == ctx.Response.StatusCode
            );

        rt.Url = ctx.Request.Uri;
        rt.HttpMethod = ctx.Request.Method;
        telemetryClient.TrackRequest(rt);
    }
}


public class InsightsExceptionLogger : ExceptionLogger
{
    readonly TelemetryClient telemetryClient;

    public InsightsExceptionLogger(TelemetryClient telemetryClient)
    {
        this.telemetryClient = telemetryClient;            
    }

    public override Task LogAsync(ExceptionLoggerContext context, System.Threading.CancellationToken cancellationToken)
    {
        telemetryClient.TrackException(context.Exception);
        return Task.FromResult<object>(null);
    }

    public override void Log(ExceptionLoggerContext context)
    {
        telemetryClient.TrackException(context.Exception);
    }
}

它们像这样注册到我们的应用程序中:

They are registered to our application like so:

static void ConfigureInsights(IAppBuilder app, HttpConfiguration config)
{
    var rtClient = new TelemetryClient();
    app.Use<InsightsReportMiddleware>(rtClient);
    config.Services.Add(typeof (IExceptionLogger), new InsightsExceptionLogger(rtClient));
}

这可行,除了异常和请求未连接.两者都被记录下来,但是当单击失败的请求时,它会显示未找到相关异常".相反,在打开异常属性时,我们可以读取受此异常影响的请求:0".这样做的正确方法是什么?

This works, except exceptions and requests are not connected. Both get logged, but when clicking on a failed request it says "No related exceptions were found". Conversely, when opening an exception properties, we can read "Requests affected by this exception: 0". What is the proper way to do that?

推荐答案

Application Insights 通过比较 ExceptionTelemetry.Context.Operation.IdRequestTelemetry.Id 来链接异常和请求.

Application Insights links exceptions and requests by comparing ExceptionTelemetry.Context.Operation.Id and RequestTelemetry.Id.

我没有 OWIN 的代码示例,但是 Application Insights SDK 的 ASP.NET 5 包具有用于跟踪异常和请求的类似中间件组件.我希望您可以利用这些信息为 OWIN 构建解决方案.

I don't have a code sample for OWIN, however the ASP.NET 5 package of the Application Insights SDK has similar middleware components for tracking exceptions and requests. I hope you can use this information to build a solution for OWIN.

在调用执行实际请求处理的下一个中间件组件之前,我们创建一个 RequestTelemetry 实例并将其存储在请求处理环境中.在 ASP.NET 5 中,我们将 RequestTelemetry 注册为请求范围的服务.使用 OWIN,我想您的中间件组件会创建它并将其存储在 environment 字典中.

We create a RequestTelemetry instance and store it in the request processing environment before invoking the next middleware component which performs actual request processing. In ASP.NET 5, we register RequestTelemetry as a request-scoped service. With OWIN, I'd imagine your middleware component would create it and store it in the environment dictionary.

我们还有一个 ITelemetryInitializer,名为 OperationIdTelemetryInitializer,使用从环境中提取的 RequestTelemetry.Id 设置 ITelemetry.Context.Operation.Id.需要将此初始化程序添加到用于在应用程序中创建 TelemetryClient 实例的 TelemetryConfiguration 中.TelemetryConfiguration.Active 默认使用.

We also have an ITelemetryInitializer, called OperationIdTelemetryInitializer, that sets the ITelemetry.Context.Operation.Id with the RequestTelemetry.Id extracted from the environment. This initializer needs to be added to the TelemetryConfiguration used to create the TelemetryClient instances in your application. TelemetryConfiguration.Active is used by default.

这篇关于如何将异常链接到 Azure 上的 Application Insights 中的请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)