15

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.

2 Answers 2

12

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.UserAgent = _httpContextAccessor.HttpContext.Request.Headers["User-Agent"];
            }
        }
    }

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

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

    services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
4

It seems that if you are particularly interested in a specific UA, you may have to collect it by yourself & at your own risk.

See https://github.com/Microsoft/ApplicationInsights-Announcements/issues/3.

Not the answer you're looking for? Browse other questions tagged or ask your own question.