Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 422
FlipflopPancake Avatar asked Jul 03 '18 12:07

FlipflopPancake


1 Answers

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"];
        }
    }
}

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>();
like image 55
jchang1 Avatar answered Sep 25 '22 05:09

jchang1