Reputation: 739
I have a custom AI Telemetry Initializer. Recently, I have started getting a compiler warning, saying Context.Properties
is obsolete:
"TelemetryContext.Properties is obsolete. Use GlobalProperties to set global level properties. For properties at item level, use ISupportProperties.Properties."
ISupportProperties.Properties
? 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;
}
}
}
}
}
Upvotes: 18
Views: 6045
Reputation: 36839
I don't see the need for a cast, you could do a type test
For example, in C# 7+:
public class MyTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
if (telemetry is ISupportProperties telemetryWithProperties)
{
telemetryWithProperties.Properties["mykey"] = "test";
}
}
}
Upvotes: 1
Reputation: 91
I had a similar problem and find this post. They added documentation for adding properties to TelemetryContext. Simply cast ITelemetry to RequestTelemetry.
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
requestTelemetry.Properties["customProp"] = "test";
}
Upvotes: 7
Reputation: 30035
1.How do I do that? Use ISupportProperties.Properties?
Just cast the ITelemetry
to ISupportProperties
.
My example code as below:
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");
}
}
}
Then after execution, the property is shown in azure portal, screenshot as below:
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?
As per my understanding, you can use support property(item level) since it's similar with you did previously using like this telemetry.Context.Properties["northstar_deviceid"] = deviceId;
Upvotes: 16