Tracking Authenticated User ID in Application Insights
After getting the basic out-of-the-box inplementation of Application Insights (AI) up and running, I decided to customize it to pass the ID of the current user along with the telemetry. The first couple of articles I discovered recommended writing a custom Telemetry Initializer to populate the Telemetry.Context.AuthenticatedUserId property. Since my website uses ASP.NET forms authentication, I was able to grab the ID of the current user from the HttpContext.Current.User property. My initializer ended up looking something like this: public class MyTelemetryInitializer : ITelemetryInitializer { public void Initialize(ITelemetry telemetry) { var identity = HttpContext.Current?.User?.Identity; if (identity == null) return; telemetry.Context.User.AuthenticatedUserId = identity.Name; } } Then I register my initializer using the following line of code in the app_start function of my Global.asax.cs file. ...
Comments
Post a Comment