我正在尝试从 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/