Skip to content

Instantly share code, notes, and snippets.

@lawrencegripper
Last active January 22, 2017 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencegripper/a59040d043dfafc483a8896c662f4f06 to your computer and use it in GitHub Desktop.
Save lawrencegripper/a59040d043dfafc483a8896c662f4f06 to your computer and use it in GitHub Desktop.
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