azure-application-insights - 在 global.asax.cs 中分配后未填充 Insights InstrumentationKey
问题描述
我有一个.NET Framework 4.6.1
使用这些 Nuget 包的 Web API 解决方案:
<package id="Microsoft.ApplicationInsights" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.Agent.Intercept" version="2.4.0" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.DependencyCollector" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.PerfCounterCollector" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.Web" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.5" targetFramework="net461" />
在global.asax.cs
我有这个代码:
protected void Application_Start()
{
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings[$"Insights-{EnvironmentConfig.EnvironmentName}"];
GlobalConfiguration.Configure(WebApiConfig.Register);
}
我可以确认ConfigurationManager.AppSettings[]
值检索确实检索到有效的 Insights 键。
在控制器中我有:
public class MyController : ApiController
{
TelemetryClient telemetryClient = new TelemetryClient(TelemetryConfiguration.Active);
[Route("api/DoSomething")]
[HttpPost]
public async Task<HttpResponseMessage> DoSomething()
{
try
{
// stuff happens
}
catch (Exception ex)
{
telemetryClient.TrackException(ex); // does not log
throw ex;
}
}
}
如果我在// stuff happens
(那里当然有实际代码)设置一个停止点并检查telemetryClient
我可以看到该InstrumentationKey
属性是空的。这意味着我需要在实例化TelemetryClient
类并故意填充它的任何地方添加一行。
我怎样才能让它从头global.asax.cs
开始填充?
解决方案
尝试了两个 appsetting 属性ConfigurationManager.AppSettings
,WebConfigurationManager.AppSettings
我可以查看 Instrumentation键TelemetryConfiguration
并登录。Application Insights
我们正在添加我们的检测密钥,TelemetryConfig
其中可用Telemetryconfig.
protected void Application_Start()
{
TelemetryConfiguration.Active.InstrumentationKey = ConfigurationManager.AppSettings[$"Insights-{EnvironmentConfig.EnvironmentName}"];
//TelemetryConfiguration.Active.InstrumentationKey = WebConfigurationManager.AppSettings[$"Insights-{EnvironmentConfig.EnvironmentName}"];
GlobalConfiguration.Configure(WebApiConfig.Register);
}
推荐阅读
- sql - 非聚集索引和复合主键之间的性能
- antlr4 - Antlr4 不匹配输入
- c# - BrowserLink 工具不适用于 ASP.NET Core 2.1?
- php - PHP + Stripe API - 从点语法动态设置 php 对象值
- python - 在 python 中,如何将具有相似键的字典元素合并?
- python - _csv.Error 的问题:预期序列
- ios - 如何在 WkWebView 中检测向后滑动
- c++ - 堆栈上的前向迭代器
- vue.js - 如何防止 Vue.js 中循环组件的数据/方法共享
- javascript - Angular - HTML5 输入类型“datetime-local”不适用于日期对象