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 添加到配置中:
var instrumentationKey= Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY ").Value;
services.AddApplicationInsightsTelemetry(opt => opt.InstrumentationKey = instrumentationKey);
编辑:
我根据 Tseng 的回答得出的结论是,在 Azure 门户和 appsettings.json 中都使用 APPINSIGHTS_INSTRUMENTATIONKEY 是最佳实践。
ASP.NET Core 理解两者
APPINSIGHTS_INSTRUMENTATIONKEY
和 ApplicationInsights: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.json
或 appsettings.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);
哪里Configuration
是 IConfigurationRoot
.如果找到,此重载将从环境变量或 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/