23

I have an Azure Website. For the sake of this question, the production version of the website runs on example.com, and a test version of the website runs on sandbox.example.com.

The only difference between the two is that they have different configuration.

At present, they are running under different websites, and I deploy the same website to each azure website via git.

I'd like to separate out the Application Insights data. Is there a technique or process that anyone uses - apart from editing the ApplicationInsights.config file in the sandbox environment post deploy?

Or would using a deployment slot handle this in some way?

Brendan Green
  • 11,676
  • 5
  • 44
  • 76

3 Answers3

36

There was a new blog post about exactly this today: Application Insights Support for Multiple Environments, Stamps and App Versions.

The destination of the telemetry is determined by the instrumentation key (iKey), which is sent along with every telemetry message. In the Application Insights portal, similar events and metrics with the same iKey are aggregated to give you charts of average durations, event counts, the sum of users, and so on. The iKey appears in two places in your project. One is in ApplicationInsights.config: <InstrumentationKey>94843456-2345-3456-4567-324562759284</InstrumentationKey>

If your application has web pages, the iKey also appears in a script in the head of every web page. Usually, it’s only coded once in a master page such as Views\Shared\_Layout.cshtml.

To direct telemetry to different application resources, we can create several resources with different iKeys. Then we only have to change the iKeys in the application at each transition in its lifecycle – along with other configuration data such as connection strings, certificates, and subscriptions.

The article then goes on how to do this in code, confg, etc:

1) Add iKey as a property in Web.config:

2) Instead of using the iKey from ApplicationInsights.config, we’ll set it in the code. In global.asax.cs.

To avoid confusion, remove the <InstrumentationKey> node from ApplicationInsights.config.

3) Configure the web pages to pick up instrumentationKey: "@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey". This is the script usually found in View\Shared\_Layout.cshtml.

4) Don’t forget to update your Web.config with appropriate iKey configuration during the deployment process. You might devise a way of setting it appropriately as part of your build, but I’ll leave that to you.

OzBob
  • 4,227
  • 1
  • 39
  • 48
John Gardner
  • 24,225
  • 5
  • 58
  • 76
  • 1
    This is precisely what I need. I am curious to know if this was an existing feature that wasn't documented (or hard to find) or if it's something new? – Brendan Green Jan 09 '15 at 02:27
  • 1
    I think most of the stuff in the blog post is stuff you could have done for a while, just not documented anywhere yet. at least by my read – John Gardner Jan 11 '15 at 04:46
  • 1
    Link is dead, here is another to the same content- https://devblogs.microsoft.com/devops/application-insights-support-for-multiple-environments-stamps-and-app-versions/ – Nathan Apr 23 '20 at 15:39
3

Found this semi-related question: How to support multiple Azure subscriptions for a single application with application insights this is for using by cloud services, and it works!

Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings["appInsightsKey"];

I have done this in my unity registertypes method, it works there.

Community
  • 1
  • 1
Dennis
  • 63
  • 1
  • 10
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – JTG Dec 22 '14 at 21:05
  • @JTG is this a better answer? – Dennis Dec 22 '14 at 21:27
  • I understand you cannot comment yet and that maybe frustrating, but that doesn't mean the answer section becomes a impromptu comment section for those who don't have enough rep. Rep locking comments is the best way for SO to stop spam. Ask good questions and/or post good answers and you will easily get the 50 rep points needed to contribute. – JTG Dec 22 '14 at 21:36
  • i know the reasons for the reputation system, and yes was a bit frustrated and i was rambling i my "answer" ... have cleaned it up to provide an answer to the question.... – Dennis Dec 24 '14 at 08:57
2

In the Azure portal for websites, on the config tab there is a section called App Settings. You can put your different configuration settings here. When publishing, azure will inject those settings into web.config.

Then just use WebConfigurationManager.AppSettings as you would normally and it will pull the injected values.

dascalos
  • 509
  • 4
  • 14
  • 3
    Yes - but the Application Insights configuration is not in `web.config`, but rather `ApplicationInsights.config`. – Brendan Green Nov 20 '14 at 21:22
  • Sorry, missed that point. Looks like you can configure applicationinsights.config per build configuration. Here's a link... http://msdn.microsoft.com/en-us/library/dn550723.aspx – dascalos Nov 22 '14 at 13:18