我有一个自定义的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;
}
}
}
}
}
答案 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");
}
}
}
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";
}