Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Websites Application Insights - switching config

Tags:

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?

like image 914
Brendan Green Avatar asked Nov 20 '14 12:11

Brendan Green


People also ask

How do I change my Azure web config?

If you want to change web. config files you can use the Azure portal, there is a tool called "App Service Editor" in preview or Kudu that lets you edit any of the files you've deployed.

How are configurations in Azure App Service configured?

Configure general settings. In the Azure portal, search for and select App Services, and then select your app. In the app's left menu, select Configuration > General settings. Here, you can configure some common settings for the app.

How do I configure Appinsights?

This section will guide you through automatically adding Application Insights to a template-based ASP.NET web app. From within your ASP.NET web app project in Visual Studio: Select Project > Add Application Insights Telemetry > Application Insights Sdk (local) > Next > Finish > Close.

Should I use single or multiple application Insights resources?

As per the FAQ: use one instance: Should I use single or multiple Application Insights resources? Use a single resource for all the components or roles in a single business system. Use separate resources for development, test, and release versions, and for independent applications.


2 Answers

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.

like image 62
John Gardner Avatar answered Oct 07 '22 09:10

John Gardner


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.

like image 27
Dennis Avatar answered Oct 07 '22 09:10

Dennis