.net - 从用户 secret 获取 ApplicationInsights 设置

标签 .net azure .net-core azure-application-insights

我发现 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/

相关文章:

c# - 具有多个内容的 WPF 样式

.net - 如何拦截编辑控件中的Tab键

azure - 如何在 Azure Blob 存储帐户的 Azure 存储资源管理器中获取到容器(文件夹)的直接链接

asp.net - 使用 .net Framework 4 及更高版本(而不是 .NET Core)的微服务

.net-core - .Net Core ILogger 将关键保存为错误

c# - 在资源管理器中的文件图标上添加状态图标,例如 .NET 中的 Dropbox 或 SVN

c# - 使用 MouseClick Winforms 控件事件实现缺少的 MouseDoubleClick

Azure:确定资源是手动部署还是通过arm模板部署

azure - Terraform 通过服务主体和客户端 key 向 azure 进行身份验证

c# - 有没有办法找到哪些任务在同一 DbContext 上运行导致错误?