HttpContext和TelemetryInitializer

时间:2016-07-16 16:11:58

标签: azure-application-insights

我想将用户的“client_id”声明作为属性附加到发送到Application Insights的每个请求。

从我读过的内容来看,我应该实现ITelemetryInitializer,但我需要HttpContext请求才能检索“client_id”。看我的初始化者:

public class ClaimTelemetryInitializer : ITelemetryInitializer
{
    public HttpContext HttpContext { get; set; }

    public void Initialize(ITelemetry telemetry)
    {
        this.AddTelemetryContextPropertFromClaims(telemetry, "client_id");
    }

    private void AddTelemetryContextPropertFromClaims(ITelemetry telemetry, string claimName)
    {
        if (HttpContext != null)
        {
            var requestTelemetry = telemetry as RequestTelemetry;

            var claim = HttpContext.User.Claims.SingleOrDefault(x => x.Type.Equals(claimName, StringComparison.InvariantCultureIgnoreCase));

            if (claim != null)
            {
                telemetry.Context.Properties[claimName] = claim.Value;
            }
        }
    }
}

我可以创建一个动作过滤器来每次设置上下文,但这感觉很糟糕:

public class TrackClaimsAttribute : ActionFilterAttribute
{
    public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        var initialiser = TelemetryConfiguration.Active.TelemetryInitializers.OfType<ClaimTelemetryInitializer>().Single();

        initialiser.HttpContext = context.HttpContext;

        return base.OnActionExecutionAsync(context, next);
    }
}

有没有更好的方法来实现我想要做的事情?

3 个答案:

答案 0 :(得分:7)

您应该实现为您提供HttpContext的WebTelemetryInitializerBase

您的代码应如下所示:

public class ClaimTelemetryInitializer : WebTelemetryInitializerBase
{
    protected override void OnInitializeTelemetry(
            HttpContext platformContext,
            RequestTelemetry rootRequestTelemetry, 
            ITelemetry telemetry) {

            var claim = HttpContext.User.Claims.SingleOrDefault(x => x.Type.Equals(claimName, StringComparison.InvariantCultureIgnoreCase));

            if (claim != null)
            {
                telemetry.Context.Properties[claimName] = claim.Value;
            }
    }
}

答案 1 :(得分:1)

我希望这是AppInsights中设计的,但是您可以直接使用静态HttpContext.Current。您可以将其每个请求的Items字典用作短期(接近无状态)存储空间,以将自定义值传递到自定义遥测处理程序。

所以尝试

class AppInsightCustomProps : ITelemetryInitializer
{
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;

        var httpCtx = HttpContext.Current;
        if (httpCtx != null)
        {
            var customPropVal = (string)httpCtx.Items["PerRequestMyCustomProp"];
            if (!string.IsNullOrWhiteSpace(customPropVal))
            {
                requestTelemetry.Properties["MyCustomProp"] = customPropVal;
            }
        }
    }
}

要对所需的自定义属性进行编程,请在请求管道中的任何地方添加类似内容

if (HttpContext.Current != null)
{
    HttpContext.Current.Items["PerRequestMyCustomProp"] = myCustomPropValue;
}

答案 2 :(得分:0)

我建议在ClaimTelemetryInitializer类的构造函数中注入HttpContextAccessor实例,然后可以使用它从HttpContext中提取值。 或者,甚至更好的是,为TelemetryInitializer创建一个基类,并使用其构造函数注入HttpContextAccessor实例。

例如:

    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...
    }