从用户机密获取 ApplicationInsights 设置
Get ApplicationInsights settings from User Secrets
我发现 ApplicationInsights.InstrumentationKey
的配置不是从用户机密中获取的。在 DefaultApplicationInsightsServiceConfigureOptions
中,我找到了这段代码:
configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
因此没有从用户机密或命令行参数中获取任何配置。
这是故意的吗?
仅仅因为存在默认值并不意味着您不能将密钥存储为用户机密等。甚至是questionable到底是不是秘密。如果密钥被泄露,唯一的风险是它被用来发送额外的遥测数据。它不能用于读取遥测数据。
也就是说,您可以更改配置:
If you want to store the instrumentation key in ASP.NET Core user secrets or retrieve it from another configuration provider, you can use the overload with a Microsoft.Extensions.Configuration.IConfiguration parameter. For example, services.AddApplicationInsightsTelemetry(Configuration);. Starting from Microsoft.ApplicationInsights.AspNetCore version 2.15.0, calling services.AddApplicationInsightsTelemetry() will automatically read the instrumentation key from Microsoft.Extensions.Configuration.IConfiguration of the application. There is no need to explicitly provide the IConfiguration.
(source)
另外,在同一个文档中,环境变量也可以:
An instrumentation key specified in code wins over the environment variable APPINSIGHTS_INSTRUMENTATIONKEY, which wins over other options.
我发现 ApplicationInsights.InstrumentationKey
的配置不是从用户机密中获取的。在 DefaultApplicationInsightsServiceConfigureOptions
中,我找到了这段代码:
configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
因此没有从用户机密或命令行参数中获取任何配置。
这是故意的吗?
仅仅因为存在默认值并不意味着您不能将密钥存储为用户机密等。甚至是questionable到底是不是秘密。如果密钥被泄露,唯一的风险是它被用来发送额外的遥测数据。它不能用于读取遥测数据。
也就是说,您可以更改配置:
If you want to store the instrumentation key in ASP.NET Core user secrets or retrieve it from another configuration provider, you can use the overload with a Microsoft.Extensions.Configuration.IConfiguration parameter. For example, services.AddApplicationInsightsTelemetry(Configuration);. Starting from Microsoft.ApplicationInsights.AspNetCore version 2.15.0, calling services.AddApplicationInsightsTelemetry() will automatically read the instrumentation key from Microsoft.Extensions.Configuration.IConfiguration of the application. There is no need to explicitly provide the IConfiguration.
(source)
另外,在同一个文档中,环境变量也可以:
An instrumentation key specified in code wins over the environment variable APPINSIGHTS_INSTRUMENTATIONKEY, which wins over other options.