Azure和ApplicationInsights配置的APPINSIGHTS_INSTRUMENTATIONKEY有什么区别:InstrumentationKey?

Lie*_*ero 11 visual-studio azure-web-sites azure-application-insights asp.net-core

Application Insight配置存在一些混淆.它可以使用Visual Studio在应用程序本身中配置,也可以在使用Azure Portal的App Service中配置.

视觉工作室

当我使用Visual Studio 将Application Insights遥测添加到我的asp.net core 2.0网站时,它将以下配置添加到appsettings.json:

{
// Changes to file post adding Application Insights Telemetry:
  "ApplicationInsights": {
    "InstrumentationKey": "10101010-1010-1010-1010-101010101010"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我在startup.cs中配置AppInsights服务,如下所示:

var instrumentationKey= Configuration.GetSection("ApplicationInsights:InstrumentationKey").Value;
services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
Run Code Online (Sandbox Code Playgroud)

Azure门户

但是,当我在Azure门户中的App Service中打开Application Insights选项卡时,它仍建议连接Application Insight.然后,向导会向配置添加新的Intrumentation Key:

在此输入图像描述

  1. 为什么有两个不同的键?
  2. 究竟什么遥测产生App Service以及.NET Core应用程序本身.
  3. 如何避免两次配置InstrumentationKey?
  4. 仅使用APPINSIGHTS_INSTRUMENTATIONKEY有什么副作用(例如在Visual Studio工具中).我的意思是我会在startup.cs中写:

    var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
    services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
    
    Run Code Online (Sandbox Code Playgroud)

编辑:

我根据Tseng的回答得出结论,最好在Azure门户和appsettings.json中使用APPINSIGHTS_INSTRUMENTATIONKEY.

ASP.NET核心既了解APPINSIGHTS_INSTRUMENTATIONKEYApplicationInsights:InstrumentationKey,但Azure的门户网站只有第一个,它必须是环境变量.如果您使用了第二个,并尝试从代码中的某个位置的config中读取它,您可以轻松地在Azure门户和在Azure中运行的应用程序中使用不同的值.

此外,如果您从配置中手动读取检测键,则应首先查看APPINSIGHTS_INSTRUMENTATIONKEY然后ApplicationInsights:InstrumentationKey:

var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY")?.Value
    ?? Configuration.GetSection("ApplicationInsights:InstrumentationKey")?.Value;
Run Code Online (Sandbox Code Playgroud)

因为那是多么services.AddApplicationInsightsTelemetry(Configuration);有效.以防万一Azure Portal中的设置密钥与appsettings.json中的设置密钥不同

Tse*_*eng 6

好吧,第一个是当您不托管在Azure App Service上或不想设置环境变量时。实际使用哪种,取决于配置构建器的配置方式。

通常您在Startup.cs或中有类似的内容Programm.cs

var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
    .AddUserSecrets<Startup>()
    .AddEnvironmentVariables(); // Environment Variables override all other
Run Code Online (Sandbox Code Playgroud)

.AddXxx呼叫的使用顺序很重要。将使用最后一个带有匹配密钥的注册。这.AddEnvironmentVariables()是最后一个。当APPINSIGHTS_INSTRUMENTATIONKEY变量被设置,它将覆盖为所有值Appinsights:InstrumentationKey集在用户的秘密,appsettings.Development.json或者appsettings.json

如果APPINSIGHTS_INSTRUMENTATIONKEY未设置,则配置库将调查用户机密并在找到时使用它。如果找不到,它将搜索appsettings.Development.json并且如果它不包含值search appsettings.json

TL; DR:仅当未设置环境变量时才使用一种形式的appsettings.json。

更新资料

新答案

代码中可以看出,注册它的Application Insight扩展方法将在找到匹配条目时覆盖环境变量或appsettings.json中的值。

注意:删除.AddEnvironmentVariables()它时,它将永远不会使用在Azure Portal中设置的值,因为.AddEnvironmentVariables()使用密钥将环境变量加载到配置中APPINSIGHTS_INSTRUMENTATIONKEY(请参见下文)。

private const string InstrumentationKeyFromConfig = "ApplicationInsights:InstrumentationKey";
private const string InstrumentationKeyForWebSites = "APPINSIGHTS_INSTRUMENTATIONKEY";
Run Code Online (Sandbox Code Playgroud)

如果在此位置找不到它,它将尝试从appsettings.json获取常规密钥ApplicationInsights:InstrumentationKey

在你的例子中

var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
Run Code Online (Sandbox Code Playgroud)

除非您同时删除环境变量(或)从中删除条目,否则不会使用传递的值。.AddEnvironmentVariables()appsettings.json

因此,对于最常见的配置,足以调用

services.AddApplicationInsightsTelemetry(Configuration);
Run Code Online (Sandbox Code Playgroud)

这里ConfigurationIConfigurationRoot。如果找到,此重载将从环境变量或appsettings.json加载。

如果您希望对其进行更多的编程控制,请使用

services.AddApplicationInsightsTelemetry(options => {
    // some logic here, where you can override the default behavior described above
});
Run Code Online (Sandbox Code Playgroud)

  • 本质上是相同的,请参见[源代码](https://github.com/Microsoft/ApplicationInsights-aspnetcore/blob/v2.5.0/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs#L288-L292) 。使用config设置options类覆盖环境变量值时,用于注册它的扩展方法在任一方法中 (2认同)