title: What's the difference between APPINSIGHTS_INSTRUMENTATIONKEY configured by Azure and ApplicationInsights:InstrumentationKey?
body: <p>There is some confusion in the Application Insight configuration. It can be configured in the application itself using Visual Studio and in the App Service using Azure Portal.</p>
  <h1>Visual Studio</h1>
  <p>When I use Visual Studio to <a href="https://docs.microsoft.com/en-us/azure/application-insights/app-insights-asp-net-core" rel="noreferrer">add Application Insights Telemetry</a> to my asp.net core 2.0 website, it adds following configuration to appsettings.json:</p>
  <pre><code>{
  // Changes to file post adding Application Insights Telemetry:
    &quot;ApplicationInsights&quot;: {
      &quot;InstrumentationKey&quot;: &quot;10101010-1010-1010-1010-101010101010&quot;
    }
  }
  </code></pre>
  <p>I then configure the AppInsights services in the startup.cs like this:</p>
  <pre><code>var instrumentationKey= Configuration.GetSection(&quot;ApplicationInsights:InstrumentationKey&quot;).Value;
  services.AddApplicationInsightsTelemetry(opt =&gt; opt.InstrumentationKey = instrumentationKey);
  </code></pre>
  <h1>Azure Portal</h1>
  <p>However, when I open Application Insights tab in the App Service in Azure Portal, it still suggest to connect Application Insight. The wizard then adds new Intrumentation Key to configuration:</p>
  <p><a href="https://i.stack.imgur.com/u9cd6.png" rel="noreferrer"><img src="https://i.stack.imgur.com/u9cd6.png" alt="enter image description here" /></a></p>
  <ol>
  <li><p>Why there are two different keys?</p>
  </li>
  <li><p>Exactly what telemetry produces App Service and what the .NET Core application itself.</p>
  </li>
  <li><p>How can I avoid having the InstrumentationKey configured twice?</p>
  </li>
  <li><p>What are the side effects (e.g. inside Visual Studio tooling) of using APPINSIGHTS_INSTRUMENTATIONKEY only. I mean I would write in startup.cs:</p>
  <pre><code>var instrumentationKey= Configuration.GetSection(&quot;APPINSIGHTS_INSTRUMENTATIONKEY &quot;).Value;
  services.AddApplicationInsightsTelemetry(opt =&gt; opt.InstrumentationKey = instrumentationKey);
  </code></pre>
  </li>
  </ol>
  <h2>EDIT:</h2>
  <p>I've come to conclusion based on Tseng's answer that it is best practice to use APPINSIGHTS_INSTRUMENTATIONKEY in both, in Azure portal and in appsettings.json.</p>
  <p>ASP.NET Core understands both <code>APPINSIGHTS_INSTRUMENTATIONKEY</code> and <code>ApplicationInsights:InstrumentationKey</code>, but Azure Portal only the first one and it has to be environment variable. If you used the second, and tried to read it from config somewhere in the code, you could easily end up with different values in Azure Portal and in your app running in Azure.</p>
  <p>Also, if you are manually reading the instrumentation key from configuration, you should first look at <code>APPINSIGHTS_INSTRUMENTATIONKEY</code> and then to <code>ApplicationInsights:InstrumentationKey</code>:</p>
  <pre><code>var instrumentationKey= Configuration.GetSection(&quot;APPINSIGHTS_INSTRUMENTATIONKEY&quot;)?.Value
      ?? Configuration.GetSection(&quot;ApplicationInsights:InstrumentationKey&quot;)?.Value;
  </code></pre>
  <p>because that's how <code>services.AddApplicationInsightsTelemetry(Configuration);</code> works as well. Just in case there would be different setting key in Azure Portal than in appsettings.json</p>