我发现 ApplicationInsights.InstrumentationKey
的配置不是从用户 key 中获取的。在 DefaultApplicationInsightsServiceConfigureOptions
中,我找到了以下代码:
configBuilder.AddJsonFile("appsettings.json", true)
.AddJsonFile(string.Format(CultureInfo.InvariantCulture, "appsettings.{0}.json", this.hostingEnvironment.EnvironmentName), true)
.AddEnvironmentVariables();
因此,无法从用户 secret 或命令行参数中获取任何配置。
这是故意的吗?
最佳答案
仅仅因为存在默认值并不意味着您不能将 key 存储为用户 secret 。甚至是questionable这是否是一个 secret 。如果 key 被泄露,唯一的风险是它被用来发送额外的遥测数据。它不能用于读取遥测数据。
也就是说,您可以更改配置:
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.
关于.net - 从用户 secret 获取 ApplicationInsights 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65128619/