gpt4 book ai didi

azure - Application Insights - 如何在 TelemetryInitializer 中使用 ISupportProperties

转载 作者:行者123 更新时间:2023-12-04 17:30:10 24 4
gpt4 key购买 nike

我有一个自定义的 AI 遥测初始化程序。最近,我开始收到编译器警告,说 Context.Properties 已过时:“TelemetryContext.Properties 已过时。使用 GlobalProperties 设置全局级别属性。对于项目级别的属性,请使用 ISupportProperties.Properties。”

  1. 我该怎么做?使用ISupportProperties.Properties
  2. 我正在根据请求记录主体中声明的租户 ID 和设备 ID。这被视为应用程序全局属性还是支持属性?

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

最佳答案

1.How do I do that? Use 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.I am logging the tenant Id, and deviceID, from claims in the principal, per request. Is that considered app global or a support property?

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

关于azure - Application Insights - 如何在 TelemetryInitializer 中使用 ISupportProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52821523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com