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.

@Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey

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;
    appInsights.trackPageView();
</script>

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

image

Related Posts

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 ...
Point to Site VPN Client won’t install To connect an Azure App Service to a on-premise database you can make use of different solutions. Two of those solutions are: Hybrid Connection ...
Invoke Azure Function in your Visual Studio Team Services CI/CD pipeline A utility task is available for Visual Studio Team Services (VSTS) to invoke an http triggered Azure function. The ability to invoke a Function from y...
VSTS Extension for Azure Role Based Access Control Today I published an extension for Visual Studio Team Services (VSTS) that gives you the ability to add and remove role based access assignments in Az...
Listing Azure Services within a CSV file In some situations you will look into a current Azure Environment and the setup/governance of it and need to migrate or move resources around. The ...
Azure Event Grid with Custom Events As of yesterday (16-8-2017) the public preview of Azure Event Grid is live. Azure Event Grid is a fully managed event routing service. Azure Event Gri...

One comment

Leave a Reply

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