Adding Application Insights To Existing Web And Service Applications

In this post I’m going to take a quick look at adding Application Insights to existing web and service applications. I’m going to highlight a few things I discovered and explore the difference between the two approaches.

Adding Application Insights During Development Or Post Deployment

There are two ways to add Application Insights to your application:

  • During development you can use the Application Insight SDK and wizard in Visual Studio to add it to your web application.
  • After deployment you can enable Application Insights using the Status Monitor application or PowerShell.

The web application I’m working on is tied to Visual Studio 2012 and Visual Studio 2013 or higher is required for the wizard in Visual Studio, hence the investigation to add Application Insights to deployed applications. Enabling Application Insights post deploy can also be handy for web applications where you don’t have access to the source or you are not allowed to make any changes. I followed the instructions here.

Some Difference Between The Two Methods

  • When you add Application Insights to your project using the wizard in Visual Studio it will install additional nuget packages, references and configuration settings to send telemetry data to Azure. For deployed applications you have to install the StatusMonitor application to configure Application Insights or use PowerShell. Application Insights will keep it’s configuration in an ApplicationInsights.config file in your web app root folder and some nuget packages in your App_Data folder and additional dlls in the bin folder.

Adding Application Insights To Existing Web And Sevice Applications

The configuration file, dlls and packages can be  be lost when you redeploy.

Side Note: You can use Application Insights locally to view telemetry data in Visual Studio without configuring Application Insights in Azure or sending any telemetry data to Azure.

  • Application Insights can monitor client side events if you add some Javascript snippets to your pages, it can provide more detailed telemetry and you can create custom telemetry entries using the SDK. For deployed applications you only get the telemetry that IIS and .NET provides by default.

How To Add Application Insights Post Deployment Using PowerShell

Obviously loosing your Application Insights configuration after deploying is not an ideal situation, luckily you can easily script the configuration using PowerShell.

Import-Module 'C:\Program Files\Microsoft Application Insights\Status Monitor\PowerShell\Microsoft.Diagnostics.Agent.StatusMonitor.PowerShell.dll'
#optional
Update-ApplicationInsightsVersion    

Start-ApplicationInsightsMonitoring -Name appName -InstrumentationKey {YourInstrumentationKeyFoundInTheAzurePortal}

#optional
Get-ApplicationInsightsMonitoringStatus

The Update command is optional and updates the version of Application Insights installed before adding it to the application. The Get command is also optional making it easy to see the status information in your VSTS deployment log.

Adding Application Insights To Service Or Desktop Applications

For non-web applications it can’t be done after deployment, you have to add the Microsoft.ApplicationInsights.WindowsServer nuget package during development and configure the InstumentationKey in your code, instructions here.

Side Note: Application Insights is billed by usage and the first GB of telemetry data is free, making it easy to experiment or even use on small applications permanently. You can use sampling to limit the telemetry data and configure a daily limit in the portal to make sure you stay in the free tier.

Francois Delport

Published by

Francois Delport

I am a cloud and devops consultant, technology fan and previously a professional C# developer with a keen interest in system design and architecture. Currently I am involved in projects using Azure, the Microsoft stack and DevOps. I am based in Melbourne, Australia. Email: [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *