可配置的 Application Insights 检测密钥
Configurable Application Insights Instrumentation Key
我怎样才能最好地配置 Application Inisghts 的 Instrumentation Key,使 Azure 管理员能够管理 MVC5 Web 应用程序的 App Services 部署设置?在 MVC 应用程序初始化中是否有某个事件应该在何处完成,或者几乎可以在任何时候完成?我也在使用 Trace Listener 集成。
默认情况下,检测密钥 (iKey) 在 ApplicationInsights.config
文件中设置。此外,如果包含 JavaScript 部分,iKey 将再次设置在 _Layout.cshtml
文件中。这是两个不同的地方,您需要管理一个 iKey。
我希望能够通过 Azure 门户的 App Services -> Application settings 选项卡管理此密钥。原因是:
- 我想部署此应用程序的多个实例,每个实例都有自己唯一的 iKey
- 我想定期更换这个iKey(因为原因)
- 我不希望这个 iKey 存储在我们的代码存储库中("dev" iKey 可以在代码存储库中)我也不希望它由我们的构建自动化管理(同样,因为原因)
这是我目前正在使用的实现,它似乎可以工作。但是,我有其他实现似乎将 iKey 设置得太早或太晚,因为它似乎会在部署到 Azure 的物理 web.config
文件中使用 iKey,而不是从 Application 设置中提取 门户中的选项卡。是否有更好的选择以最佳实践方式执行此操作?
ApplicationInsights.config
<!-- Find the following node and *remove* it. It will have a GUID in it.
If you leave this, you may receive some errors even with all of the
other changes. -->
<InstrumentationKey>{GUID HERE}</InstrumentationKey>
Global.asax.cs
protected void Application_Start()
{
// Add this first line below
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
ConfigurationManager.AppSettings["ai:InstrumentationKey"];
// Showing the rest of this so you can see the order of operations
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
AutomapperConfig.Init();
}
web.config
<!-- Add the following to <appSettings> and put your iKey value in here. -->
<add key="ai:InstrumentationKey" value="*****" />
_Layout.cshtml(在 HTML 的 <head>
部分。未来读者请注意:我建议您不要使用此整个代码段,而是只使用以 instrumentationKey:
开头的行,并将该行集成到此 JS 代码段其余部分的任何现代版本中!):
<script type = 'text/javascript' >
var appInsights=window.appInsights||function(config)
{
function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
}({
instrumentationKey:'@(Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey)'
});
window.appInsights=appInsights;
appInsights.trackPageView();
</script>
您指定的所有方法都很棒。我们的建议是使用 web.config 应用程序设置并在 global.asax.cs 中使用它进行标准初始化。当我们连接到 OnBeginRequest() 时,在初始化之前不会发送遥测数据。
另一种可能有效的方法是设置 APPINSIGHTS_INSTRUMENTATIONKEY
SDK 获取的环境变量。当然这取决于你是否在同一台机器上有多个应用程序。
我怎样才能最好地配置 Application Inisghts 的 Instrumentation Key,使 Azure 管理员能够管理 MVC5 Web 应用程序的 App Services 部署设置?在 MVC 应用程序初始化中是否有某个事件应该在何处完成,或者几乎可以在任何时候完成?我也在使用 Trace Listener 集成。
默认情况下,检测密钥 (iKey) 在 ApplicationInsights.config
文件中设置。此外,如果包含 JavaScript 部分,iKey 将再次设置在 _Layout.cshtml
文件中。这是两个不同的地方,您需要管理一个 iKey。
我希望能够通过 Azure 门户的 App Services -> Application settings 选项卡管理此密钥。原因是:
- 我想部署此应用程序的多个实例,每个实例都有自己唯一的 iKey
- 我想定期更换这个iKey(因为原因)
- 我不希望这个 iKey 存储在我们的代码存储库中("dev" iKey 可以在代码存储库中)我也不希望它由我们的构建自动化管理(同样,因为原因)
这是我目前正在使用的实现,它似乎可以工作。但是,我有其他实现似乎将 iKey 设置得太早或太晚,因为它似乎会在部署到 Azure 的物理 web.config
文件中使用 iKey,而不是从 Application 设置中提取 门户中的选项卡。是否有更好的选择以最佳实践方式执行此操作?
ApplicationInsights.config
<!-- Find the following node and *remove* it. It will have a GUID in it.
If you leave this, you may receive some errors even with all of the
other changes. -->
<InstrumentationKey>{GUID HERE}</InstrumentationKey>
Global.asax.cs
protected void Application_Start()
{
// Add this first line below
Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey =
ConfigurationManager.AppSettings["ai:InstrumentationKey"];
// Showing the rest of this so you can see the order of operations
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
AutomapperConfig.Init();
}
web.config
<!-- Add the following to <appSettings> and put your iKey value in here. -->
<add key="ai:InstrumentationKey" value="*****" />
_Layout.cshtml(在 HTML 的 <head>
部分。未来读者请注意:我建议您不要使用此整个代码段,而是只使用以 instrumentationKey:
开头的行,并将该行集成到此 JS 代码段其余部分的任何现代版本中!):
<script type = 'text/javascript' >
var appInsights=window.appInsights||function(config)
{
function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
}({
instrumentationKey:'@(Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.InstrumentationKey)'
});
window.appInsights=appInsights;
appInsights.trackPageView();
</script>
您指定的所有方法都很棒。我们的建议是使用 web.config 应用程序设置并在 global.asax.cs 中使用它进行标准初始化。当我们连接到 OnBeginRequest() 时,在初始化之前不会发送遥测数据。
另一种可能有效的方法是设置 APPINSIGHTS_INSTRUMENTATIONKEY
SDK 获取的环境变量。当然这取决于你是否在同一台机器上有多个应用程序。