Wednesday, November 12, 2014

Adding Azure Application Insights to a Web Site

If you're developing a Web Application, you can use Visual Studio 2013.3 to automatically add all the libraries and configurations that "Azure Application Insights" needs to work.
But what about Web Sites? If you create a Web Site (or you have to manage / update one) you'll notice that you don't have such option. So, what can we do it? How can we achieve the same results? How can we integrate Azure Application Insights to a Web Site?
Just follow these steps:
  1. Create a new "Application Insights" service using the new Azure portal (preview)
  2. Copy the JavaScript code snippet proposed by the portal and add it to all the pages you want to monitor (or to the master page, if you have one)
  3. In Visual Studio 2013.3, create a new empty web application and add to it the Application Insights using the contextual menu
  4. Copy the following files from the "bin" folder of the Web App to the "bin" folder of the Web Site:
    (if you want you can copy also the related .xml and .pdb files)
  5. Return to the Azure portal (preview), go to the Application Insights service you've created before, click on the "Properties" button and copy the value of "Instrumentation Key" textbox
  6. Copy the ApplicationInsights.config file from the root of the Web App to the root folder of the Web Site
  7. In this file, replace the value of the "InstrumentationKey" key with the one copied at point 5
  8.  Change the web.config of the website adding these rows:
          <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" />
        <validation validateIntegratedModeConfiguration="false" />
     <modules runAllManagedModulesForAllRequests="true">
          <remove name="ApplicationInsightsWebTracking" />
          <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Extensibility.Web.RequestTracking.WebRequestTrackingModule, Microsoft.ApplicationInsights.Extensibility.Web" preCondition="managedHandler" />
Now you can test your Web Site and, after few seconds, you will see you analytics data in the Application Insights blade of the new Azure portal (preview)

No comments: