Thursday, February 8, 2018

Application Insights–Use a different instrumentation key per environment

By default when you configure Application Insights, the Instrumentation key is hardcoded in the JavaScript snippet


and the ApplicationInsights.config:


This makes it very hard to integrate your application into your devops pipeline and use different instrumentation keys for different environments(development, test, production).

Let’s see how we can fix this:

  • Start by commenting out the InstrumentationKey setting in the ApplicationInsights.config:


  • Instead add an extra appsetting to your web. config. By using the web.config you can use the web.config transformations to inject different instrumentation keys for different environments.


  • Now we have to tell Application Insights to use the key specified in our web.config. Therefore add the following line of code to the Application_Start method of the global.asax:
  • As a last step we also have to fix this for the JavaScript snippet injected into our page:

    No comments: