Azure Application Insights and Static Content

Published on Tuesday, March 20, 2018

As you may have noticed, this site uses Wyam as a static site generator to produce what you see here from a set of .md Markdown files checked into a Git repository.

The site is hosted on Azure Web Apps which is basically IIS under-the-hood.

There are some little tricks in the web.config file to make this work properly. Which you can find more info on the Azure hosting Wyam documentation page.

I noticed that even after instrementing Application Insights into the client side JavaScript I was not getting any server side telemetry. The reason for that is that the default Web App extension assumes that you are using ASP.NET which with a Static Site you are not.

Enabling Application Insights for Static Content

  1. If you are using Azure Web Apps. Add the Applications Insights Extension to the Web App. This will add the proper DLLs to the Bin directory of your site.

  2. Add the following code to your web.config file

     <configuration>
         <system.webServer>
             <modules runAllManagedModulesForAllRequests="true">
                 <remove name="ApplicationInsightsWebTracking"/>
                 <add name="ApplicationInsightsWebTracking" 
                 type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
                 preCondition="managedHandler"/>
             </modules>
         </system.webServer>
     </configuration>
    

    The key difference in this code compared to the Application Insights default settings is the runAllManagedModulesForAllRequests="true" flag on the Modules configuration.

And you are done! You should now see some server side telemetry for .png or .js requests in your Application Insights portal.

Thanks for reading.

Comments