本文介绍了将自定义属性添加到默认请求遥测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在应用程序见解中将自定义属性添加到默认请求遥测中?我可以通过创建新的遥测客户端来做到这一点,但由于创建重复事件,所以我不希望这样做.

How can I add custom properties to the default request telemetry in application insights? I was able to do that by creating new telemetry client but I would like NOT to do that as it creates duplicate events.

推荐答案

制作自己的自定义TelemetryInitializer. https://azure.microsoft.com/zh-CN/documentation/articles/app-insights-api-custom-events-metrics/#telemetry-initializers .

Make your own custom TelemetryInitializer. https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/#telemetry-initializers.

从以上文章中摘录:

using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MvcWebRole.Telemetry
{
  /*
   * Custom TelemetryInitializer that overrides the default SDK
   * behavior of treating response codes >= 400 as failed requests
   *
   */
  public class MyTelemetryInitializer : ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;
        int code;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
        if (!parsed) return;
        if (code >= 400 && code < 500)
        {
            // If we set the Success property, the SDK won't change it:
            requestTelemetry.Success = true;
            // Allow us to filter these requests in the portal:
            requestTelemetry.Context.Properties["Overridden400s"] = "true";
        }
        // else leave the SDK to set the Success property
    }
  }
}

然后通过配置文件或代码加载该初始化程序,有关详细信息,请参见上面的文档.

then load that initializer either in the config file or via code, see the doc above for those details.

这篇关于将自定义属性添加到默认请求遥测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 19:16