Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class AppInsightsTelemetryInitializer : ITelemetryInitializer
{
public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry)
{
var userId = HttpContext.Current?.User?.Identity?.GetUserId();
telemetry.Context.User.AccountId = userId;
CheckAndAdd(telemetry, "UserId", userId);
CheckAndAdd(telemetry, "InstanceId", Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"));
}
private void CheckAndAdd(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry, string key, string value)
{
if (!telemetry.Context.Properties.ContainsKey(key))
{
telemetry.Context.Properties.Add(key, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment