Posts

Showing posts from 2019

Microsoft Azure Application Insights

Introduction I recently set out to integrate Application Insights (AI) into my ASP.NET MVC 5 (not core, yet) web application hosted in Microsoft Azure.  I was sorely disappointed by the lack of in-depth documentation provided by Microsoft.  Whats more, most of the articles I found on the topic were outdated by several years.  I was able to piece together enough content online to get up-and-running, but had to dig into the source code to fully understand how to configure AI the way that I wanted it.  I'm going post a few little snippets here that I discovered along the way in the hopes that they help anyone else struggling to configure AI. Tracking Authenticated User ID in Application Insights

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. ...