'User-agent information in Azure 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.



Solution 1:[1]

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 the User-Agent from the each HttpRequest and sets it to the Request Telemetry's UserId field.

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>();

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 angularsen
Solution 2 CXuesong