Application Insights configurable instrumentation key

Starting with Application Insights can be done by adding NuGet Packages to your project.

Application Insights Nuget Package

In the above screenshot you see two NuGet Packages:

  • Microsoft.ApplicationInsights.JavaScript: NuGet Package for JavaScript applications.
  • Microsoft.ApplicationInsights.Web: NuGet Package for .Net web applications.

By adding both packages you can use application insights in for example a MVC application. Adding those NuGet packages will add a default JavaScript section within the “_Layout.cshtml” file. This default section will contain a instrumentation key that points to the correct Application Insights instance in Azure. This key is added as a string value.

When running multiple instances of your application (development, test, acceptance and production), you don’t want all events to be registered in the same Application Insights instance. You can change the string value on every deploy but you can also make it configurable.

When you start off the “_Layout.cshtml” file looks like this.

Instrumentation Key

To make that section configurable we open the “Global.asax” file in the project and add the following using statement.

using Microsoft.ApplicationInsights.Extensibility;

With this using statement you can set specific configurable values of application insights and in special the instrumentation key. After the using is added we add we add the application start event to the file and set the “TelemetryConfiguration” “InstrumentationKey” to a key we specify in the “Web.Config” file.

protected void Application_Start()
    TelemetryConfiguration.Active.InstrumentationKey = WebConfigurationManager.AppSettings[AppSettingKeys.InstrumentationKey];

When this value is set the below line will get the specified instrumentation key in you Razor files.


The string value of the instrumentation key can be replaced by the above line and the complete JavaScript reference for application insights will then look like this.

<script type="text/javascript">
    var appInsights = window.appInsights || function (config) {
        function s(config) { t[config] = function () { var i = arguments; t.queue.push(function () { t[config].apply(t, i) }) } } var t = { config: config }, r = document, f = window, e = "script", o = r.createElement(e), i, u; for (o.src = config.url || "//az416426.vo.msecnd.net/scripts/a/ai.0.js", r.getElementsByTagName(e)[0].parentNode.appendChild(o), t.cookie = r.cookie, t.queue = [], i = ["Event", "Exception", "Metric", "PageView", "Trace"]; i.length;) s("track" + i.pop()); return config.disableExceptionTracking || (i = "onerror", s("_" + i), u = f[i], f[i] = function (config, r, f, e, o) { var s = u && u(config, r, f, e, o); return s !== !0 && t["_" + i](config, r, f, e, o), s }), t
    instrumentationKey: '@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey'

    window.appInsights = appInsights;

When this option is configured and deployed the instrumentation key can be altered by using the Azure Portal.


Related Posts

Azure DevOps Automation A couple of weeks ago the rename / rebranding of Visual Studio Team Services to Azure DevOps was announced. The rebranding is a great step forward int...
AKS (Kubernetes) and no connection could be made because the target machine acti... A client of my had an error while connecting to different resources within their Kubernetes cluster in Azure (AKS). Kubectl error On the kubectl com...
Kubernetes (AKS) attached to Azure Storage (Files) Kubernetes (AKS) can be used for many situations. For a client we needed to make files available trough a Kubernetes Pod. The files needed to be share...
Resource Group deployment via ARM templates When deploying an Azure Resource Manager (ARM) template you have to create a resource group within Azure. To deploy a template via script your script ...
Azure Managed Service Identity and Local Development Instead of storing user credentials of an external system in a configuration file, you should store them in the Azure Key Vault. Before MSI (Managed S...
The securitydata Azure resource group Most of the times companies have rules in place for managing their Azure environment. The main rules that should be in place are “Azure Policies” and ...

One Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.