Tracking Asp.Net session in Application Insights

Azure Application Insights is a great way to get to know your applications, both from the outside (how is the user experience) and from the inside (how is your code performing).

We use it a lot to find out the slowest code and the slowest pages. And we have added a listener to the diagnostic tracing, so now we have a good insight in tracing errors.

Another nice feature is that we can follow the user during their travels through our site. Each web page, each javascript call, each database query and each external HTTP call is linked by a number of telemetry values like Session Id, User Id and OperationId.

But we had one issue with the identification of related items, the specific identification is not clear in the browser: the Application Insights identification is not available in the client. And the AI sessionid is not an Asp.Net sessionid.

We solved it by tracing explicitly the Asp.Net session. Because we can show it in the browser using Razor:

<div>Session = @Session.SessionID</div>


And we can find the sessionid in Application Insights when traced.

It’s not dangerous to expose this id on a page, the sessionid can already be found in every website when checking the communication or the cookies, so no extra security breach here :-).

There was only one extra point of attention. As long as the session is not actively used in code, each call get’s another sessionid… it is not very unique yet.

You can solve this in two ways.

First, just write a (dummy) value into the session:

Session["dummy"] ="1234";

The second, a bit more formal way, is forcing the creation of a unique session in the global asax:

public class MvcApplication : System.Web.HttpApplication
    protected void Application_Start()    
    protected void Session_Start()
        // nothing to see here. Just to keep same session
        var tt = new TelemetryTracker();
        tt.TrackTrace("TrackTraceSession " + Session.SessionID
                      , SeverityLevel.Information);

This method is only executed once, when the user calls a page for the first time.

So now we know for sure which (Asp.Net) session the user is using.


And we can track the user as far as needed. And this is also a great help during development.