Application Insights-如何在TelemetryInitializer中使用ISupportProperty

时间:2018-10-15 17:03:54

标签: azure-application-insights

我有一个自定义的AI Telemetry Initializer。最近,我开始收到编译器警告,说Context.Properties已过时: “ TelemetryContext.Properties已过时。请使用GlobalProperties设置全局级别的属性。对于项目级别的属性,请使用ISupportProperties.Properties。 1.我该怎么做?使用ISupportProperties.Properties? 2.我正在根据请求记录委托人中的声明中的租户ID和deviceID。该应用程序是全球性的还是支持性的?

    public class JsonTelemetryInitializer : ITelemetryInitializer
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly SystemOptions _systemOptions;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="accessor">For accessing the http context</param>
        /// <param name="systemOptions">System options</param>
        public JsonTelemetryInitializer(IHttpContextAccessor accessor, IOptions<SystemOptions> systemOptions)
        {
            _httpContextAccessor = accessor;
            _systemOptions = systemOptions.Value;
        }

        /// <summary>
        /// Initialize the custom telemetry initializer
        /// </summary>
        /// <param name="telemetry">Telemetry</param>
        public void Initialize(ITelemetry telemetry)
        {
            if (_httpContextAccessor.HttpContext == null)
            {
                return;
            }

            if (_httpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
            {
                const string tenantId = "northstar_tenantid";
                if (!telemetry.Context.Properties.ContainsKey(tenantId))
                {
                    var user = _httpContextAccessor.HttpContext.User;
                    telemetry.Context.Properties[tenantId] =
                        ClaimsHelper.GetClaim<int>(user, TokenNames.TenantId).ToString();

                    var userId = ClaimsHelper.GetClaim<int>(user, TokenNames.UserId).ToString();

                    telemetry.Context.Properties["northstar_userid"] = userId;
                    var deviceId = ClaimsHelper.GetClaim<string>(user, TokenNames.DeviceId);
                    if (deviceId != null)
                    {
                        telemetry.Context.Properties["northstar_deviceid"] = deviceId;
                    }

                    telemetry.Context.User.Id = userId;

                    var sessionId = ClaimsHelper.GetClaim<string>(user, TokenNames.SessionId);
                    if (!string.IsNullOrEmpty(sessionId))
                    {
                        telemetry.Context.Session.Id = sessionId;
                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:6)

  

1。我该怎么做?使用ISupportProperties.Properties?

只需将ITelemetry投射到ISupportProperties

我的示例代码如下:

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

  public class MyTelemetryInitializer:ITelemetryInitializer
  {

     public void Initialize(ITelemetry telemetry)
     {
          //cast ITelemetry to ISupportProperties
          ISupportProperties propTelemetry = (ISupportProperties)telemetry;

          if (!propTelemetry.Properties.ContainsKey("isuport_key"))
          {
              propTelemetry.Properties.Add("isuport_key", "isupport_value");
          }

      }
  }

然后在执行后,该属性显示在azure门户中,屏幕截图如下: enter image description here

  

2。我正在按请求记录委托人中的声明中的租户ID和deviceID。该应用程序是全球性的还是支持性的?

根据我的理解,您可以使用支持属性(项目级别),因为它与您之前使用过的telemetry.Context.Properties["northstar_deviceid"] = deviceId;

类似

答案 1 :(得分:1)

我遇到了类似的问题并找到了这篇文章。 他们添加了用于向 TelemetryContext 添加属性的文档。 只需将 ITelemetry 转换为 RequestTelemetry。

public void Initialize(ITelemetry telemetry)
{
    var requestTelemetry = telemetry as RequestTelemetry;
    
    if (requestTelemetry == null) return;

    requestTelemetry.Properties["customProp"] = "test";
}

https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#addmodify-properties-itelemetryinitializer