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.
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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With