Azure Application Insights中的用户代理信息 [英] User-agent information in Azure Application Insights

查看:69
本文介绍了Azure Application Insights中的用户代理信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么在.NET应用程序中实现Application Insights时不会收集用户代理信息,却能够在浏览器上收集统计信息吗?

Does anyone know why Application Insights would not be gathering user-agent information when implemented within a .NET application, yet is able to gather stats on browsers?

我一直希望能够针对特定的用户代理字符串过滤出请求,但是看起来我无法看到具有任何可用数据/表的用户代理.

I was kind of hoping to be able to filter out requests against a specific user-agent string, but looks like I'm unable to see user-agent with any of the available data/tables.

推荐答案

SDK不再是自动的.您必须通过创建自定义TelemetryInitializer自己将其包括在内.

This is no longer automagic with the SDK. You'll have to include it yourself thru creating a custom TelemetryInitializer.

public class MyCustomTelemetryInitializer: ITelemetryInitializer
{
    readonly IHttpContextAccessor _httpContextAccessor;

    public MyCustomTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }

    public void Initialize(ITelemetry telemetry)
    {
        if (telemetry is RequestTelemetry requestTelemetry)
        {
            requestTelemetry.Context.User.Id = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"];
        }
    }
}

这将从每个HttpRequest中读取用户代理,并将其设置为请求遥测的UserId字段.

This reads the the User-Agent from the each HttpRequest and sets it to the Request Telemetry's UserId field.

接下来,您需要在应用启动期间通过DI注册自定义遥测初始化程序(如果您使用的是ASP .NET Core).

Next, you'll need register your custom Telemetry Initializer during app startup via DI (if you're using ASP .NET core).

services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();

这篇关于Azure Application Insights中的用户代理信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆