VS Application Insights for a Web App deployed to multiple environments
Here is what we did.
- Create 4 AI applications
- In our ApplicationInsights.config we set it to our development componentId.
- For Test, Stage, and Prod we use a build script that replaces the componentId and componentName based on which environment we are in.
In layout javascript get the appId:
appInsights.start("@ServerAnalytics.ApplicationInsightsId");
I found this on msdn blogs from January 7, 2015 Application Insights support for Multiple Environments, Stamps and App Versions.
Basically, you can remove the instrumentation key from ApplicationInsights.config
and put it in Web.config
as an appSetting and then set it at startup.
This means that you can keep the configuration for each environment directly in azure.
My steps:
- Delete
<InstrumentationKey>
fromApplicationInsights.config
Add the setting in Web.config
<add key="appInsightsInstrumentationKey" value="id_from hre"/>
- Add settings in http://portal.azure.com for Dev, Sta, etc
On startup:
var aiInstrumentationKey = System.Web.Configuration.WebConfigurationManager.AppSettings["appInsightsInstrumentationKey"];if( string.IsNullOrEmpty(aiInstrumentationKey)){ throw new ApplicationException("appInsightsInstrumentationKey missing in web.config");}Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = aiInstrumentationKey;