Search code examples
azure-application-insightsappinsights

Can't we set Instrumentation Key on web.config instead of ApplicationInsights.Config


Instead of adding the Instrumentation Key or its connection string on the ApplicationInsights.config, Can't we add it in the web.config?

For every different deployment, we need to set the Instrumentation key manually on the ApplicationInsights.config file. Instead of, if we add it in the web config, it will take it from the Environment variables and life makes it easier for the deployment team.


Solution

  • Just add it to the initialization code of your application as explained here.

    protected void Application_Start()
    {
        Microsoft.ApplicationInsights.Extensibility.
        TelemetryConfiguration.Active.InstrumentationKey =
            // - for example -
            WebConfigurationManager.Settings["ikey"];
        ...
    }
    

    Also, if you deploy to an Azure App Service the instrumentation key is read from the Configuration -> Application Settings for most .Net applications. No need to set it in the deployment pipeline.