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

时间:2015-05-27 14:25:23

标签: c# asp.net-web-api azure-web-sites owin azure-application-insights

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

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);
    }
}

他们在我们的应用程序中注册如下:

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

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

4 个答案:

答案 0 :(得分:9)

Application Insights通过比较ExceptionTelemetry.Context.Operation.IdRequestTelemetry.Id来链接例外和请求。

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

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

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

答案 1 :(得分:8)

我最终做了什么:

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 ctx = new OwinContext(environment);
        var rt = new RequestTelemetry()
        {
            Url = ctx.Request.Uri,
            HttpMethod = ctx.Request.Method,
            Name = ctx.Request.Path.ToString(),
            Timestamp = DateTimeOffset.Now
        };
        environment.Add("requestTelemetry", rt);

        var sw = new Stopwatch();
        sw.Start();
        await next(environment);
        sw.Stop();

        rt.ResponseCode = ctx.Response.StatusCode.ToString();
        rt.Success = ctx.Response.StatusCode < 400;
        rt.Duration = sw.Elapsed;
        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)
    {
        var owinContext = context.Request.GetOwinEnvironment();
        ExceptionTelemetry exceptionTelemetry = null;
        if (owinContext != null)
        {
            object obj;
            if (owinContext.TryGetValue("requestTelemetry", out obj))
            {
                var requestTelemetry = obj as RequestTelemetry;
                exceptionTelemetry = new ExceptionTelemetry(context.Exception)
                {
                    Timestamp = DateTimeOffset.Now
                };
                exceptionTelemetry.Context.Operation.Id = requestTelemetry.Id;
            }
        }

        if (exceptionTelemetry != null)
        {
            telemetryClient.TrackException(exceptionTelemetry);
        }
        else
        {
            telemetryClient.TrackException(context.Exception);                
        }

        return Task.FromResult<object>(null);
    }

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

答案 2 :(得分:1)

对于我现在的客户,我们还没有完成任务。

我在WebAPI上注册了DelegatingHandler,通过SynchronizationContext将当前请求粘贴到帖子的CallContext.LogicalSetData中,并在请求完成后将其删除。

在我现有的日志记录系统中,必须使用Application Insights的东西进行改编,然后我通过CallContext.LogicalSetData从线程中获取请求并设置获取放入的HttpContext框架的请求属性,然后从HttpContext.Items,我得到RequestTelemetry实例。

最终,这一切都是必需的,因为我无法访问IoC容器中的请求或操作或任何新的服务。

最终,我们可能会重写其中一些内容,以便改进OperationContextInstrumentationContext样式对象的创建和流动,并摆脱处理程序和CallContext有趣的业务。< / p>

答案 3 :(得分:0)

TelemetryClient.TrackException方法有一个重载,它接受属性字典。它专门用于分类和搜索异常。这样可以生成错误ID,并将错误链接到AppInsights。

错误处理的示例:

var errorId = GenerateErrorId();

var trackProperties = new Dictionary<string, string>();
trackProperties.Add("ErrorId", errorId);

var ai = new TelemetryClient();
ai.TrackException(exception, trackProperties);

JObject resp = new JObject();
resp["message"] = exception.Message + " - " + errorId;

await context.Response.WriteAsync(resp.ToString());