c# - ITelemetry.InstrumentationKey 为空

标签 c# azure .net-core azure-application-insights telemetry

我正在尝试从 appsettings.json 设置 AppInsights TTelemetry 的 InstrumentationKey。我的代码如下所示

程序.cs

 WebHost.CreateDefaultBuilder(args)
            .UseApplicationInsights()
            .UseStartup<Startup>()
               .ConfigureAppConfiguration((hostingContext, config) =>
               {
                   config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
               });

Startup.cs,ConfigureServices()

        services.AddApplicationInsightsTelemetry(Configuration);

appsettings.json

  "ApplicationInsights": {
    "InstrumentationKey": "[my key]"
  },

但是,当我运行 _serviceProvider.GetService() 或通过 Controller 中的依赖注入(inject)接收 TelemetryClient 时,telemetry.Context.InstrumentationKey 为空。

最佳答案

这是设计使然。

TelemetryClient.InstrumentationKey or TelemetryClient.Context.InstrumentationKey应该为空,除非您明确将其设置为覆盖配置中的内容。

如上所述,显式设置为:TelemetryClient client = new TelemetryClient() { InstrumentationKey= "your_ikey" }; ,然后就可以使用.Context.InstrumentationKey来获取 key 了。

您可以找到此issue的详细信息.

希望有帮助。

关于c# - ITelemetry.InstrumentationKey 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54873229/

相关文章:

c# - 在使用 Fluent NHibernate 映射时对接口(interface)进行编程

c# - 应用程序配置无效 - 应用程序无法启动

azure - 尝试创建 Azure Redis 缓存时出现 "There are no locations available."

用于 dotnetcore2.0 的 DockerFile,具有多个 cs 项目引用

c# - 带参数的 RedirectToAction

c# - 将计时器与 Windows 服务一起使用是否明智?

azure - az ml dataset create 上的“create”拼写错误或未被系统识别

Azure 搜索 Multi-Tenancy 策略、成本和建议

c# - 根据上下文源将 Serilog 日志过滤到不同的接收器?

postgresql - EF Core FromSQL 查询不会立即执行 (PostgreSQL)