I want to attach the user\'s \"client_id\" claim as a property to every request sent to Application Insights.
From what I\'ve read, I should be implementing IT
I would suggest to inject an HttpContextAccessor instance in the ClaimTelemetryInitializer class's constructor, and then you could use it to extract values from the HttpContext. Or, even better, create a base class for your TelemetryInitializer, and use it's constructor to inject the HttpContextAccessor instance.
For example:
protected ClaimTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
this.httpContextAccessor = httpContextAccessor;
}
public void Initialize(ITelemetry telemetry)
{
var context = this.httpContextAccessor.HttpContext;
if (context == null)
{
return;
}
var claim = context.User.Claims.SingleOrDefault(x => x.Type.Equals(claimName, StringComparison.InvariantCultureIgnoreCase));
//Do logic here...
}