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

标签 c# visual-studio asp.net-core azure-web-app-service azure-application-insights

Application Insight 配置中存在一些混淆。它可以使用 Visual Studio 在应用程序本身中进行配置,也可以使用 Azure 门户在应用服务中进行配置。
视觉工作室
当我使用 Visual Studio 到 add Application Insights Telemetry到我的 asp.net core 2.0 网站,它在 appsettings.json 中添加了以下配置:

{
// Changes to file post adding Application Insights Telemetry:
  "ApplicationInsights": {
    "InstrumentationKey": "10101010-1010-1010-1010-101010101010"
  }
}
然后我在 startup.cs 中配置 AppInsights 服务,如下所示:
var instrumentationKey= Configuration.GetSection("ApplicationInsights:InstrumentationKey").Value;
services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
Azure 门户
但是,当我在 Azure 门户中的应用服务中打开 Application Insights 选项卡时,它仍然建议连接 Application Insight。然后向导将新的 Intrumentation Key 添加到配置中:
enter image description here
  • 为什么有两个不同的键?
  • 遥测生成应用服务的确切内容以及 .NET Core 应用程序本身的内容。
  • 如何避免将 InstrumentationKey 配置两次?
  • 仅使用 APPINSIGHTS_INSTRUMENTATIONKEY 的副作用是什么(例如在 Visual Studio 工具中)。我的意思是我会在 startup.cs 中写:
    var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
    services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
    

  • 编辑:
    我根据 Tseng 的回答得出的结论是,在 Azure 门户和 appsettings.json 中都使用 APPINSIGHTS_INSTRUMENTATIONKEY 是最佳实践。
    ASP.NET Core 理解两者 APPINSIGHTS_INSTRUMENTATIONKEYApplicationInsights:InstrumentationKey ,但 Azure 门户只是第一个,它必须是环境变量。如果您使用第二个,并尝试从代码中某处的 config 读取它,您很容易在 Azure 门户和在 Azure 中运行的应用程序中得到不同的值。
    此外,如果您从配置中手动读取检测 key ,您应该首先查看 APPINSIGHTS_INSTRUMENTATIONKEY然后到 ApplicationInsights:InstrumentationKey :
    var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY")?.Value
        ?? Configuration.GetSection("ApplicationInsights:InstrumentationKey")?.Value;
    
    因为这就是 services.AddApplicationInsightsTelemetry(Configuration);也有效。以防万一 Azure 门户中的设置 key 与 appsettings.json 中的设置 key 不同

    最佳答案

    嗯,第一个是当你不托管在 Azure 应用服务上或者你不想设置环境变量时。实际使用哪一个,取决于您的配置构建器的配置方式。
    通常你在 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
    
    .AddXxx的顺序电话都是用的事。将使用具有匹配 key 的最后一次注册。这里.AddEnvironmentVariables()是最后一个。当APPINSIGHTS_INSTRUMENTATIONKEY设置了变量,它将覆盖 Appinsights:InstrumentationKey 的所有值设置用户 secret ,appsettings.Development.jsonappsettings.json .
    APPINSIGHTS_INSTRUMENTATIONKEY未设置,配置库将查看用户 secret 并在找到时使用它。如果没有找到,它将搜索 appsettings.Development.json如果它不包含值搜索 appsettings.json .
    TL;博士 : 一种形式的 appsettings.json 只会在没有设置环境变量时使用。
    更新
    新答案
    the code 中所见,用于注册它的 Application Insight 扩展方法将在找到匹配条目时覆盖来自环境变量或 appsettings.json 的值。
    备注 :当您删除 .AddEnvironmentVariables() 时它永远不会使用 Azure 门户中设置的值,因为 .AddEnvironmentVariables()使用 key APPINSIGHTS_INSTRUMENTATIONKEY 将环境变量加载到配置中(见下文)。
    private const string InstrumentationKeyFromConfig = "ApplicationInsights:InstrumentationKey";
    private const string InstrumentationKeyForWebSites = "APPINSIGHTS_INSTRUMENTATIONKEY";
    
    如果在那里找不到它,它会尝试 appsettings.json ApplicationInsights:InstrumentationKey 中的常规 key .
    在你的例子中
    var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
    services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
    
    传递的值 不会被使用 除非你们俩,否则删除环境变量(或 .AddEnvironmentVariables() )appsettings.json 中删除条目.
    所以对于最常见的配置,它足以调用
    services.AddApplicationInsightsTelemetry(Configuration);
    
    哪里ConfigurationIConfigurationRoot .如果找到,此重载将从环境变量或 appsettings.json 加载它。
    当您想要对其进行更多编程控制时,您可以使用
    services.AddApplicationInsightsTelemetry(options => {
        // some logic here, where you can override the default behavior described above
    });
    

    关于c# - Azure 配置的 APPINSIGHTS_INSTRUMENTATIONKEY 和 ApplicationInsights :InstrumentationKey? 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50717398/

    相关文章:

    c# - 将多种文件类型保存到一个文件中

    C# 根据日期(月)和组拆分查询结果

    visual-studio - MSBuild "The windows SDK version 10.0.19041.0 was not found",即使已安装

    c# - 错误 CS0266 : Cannot implicitly convert type 'object' to 'int'

    c# - INSERT INTO 语句中的语法错误

    android - 是否有用于 Visual Studio 的 AndroidManifest 编辑器

    python - pip安装tornado在VS2017中失败

    c# - Asp.Net Core 未找到 DefaultChallengeScheme

    iis - web.config 中的 Asp.Net Core 标签导致失败

    ms-access - .NET Core 中的 MS Access 数据库