Jax*_*ian 4 asp.net-mvc azure asp.net-mvc-5 azure-application-insights
如何才能最好地使Application Inisghts的Instrumentation Key可以以允许Azure管理员管理MVC5 Web应用程序的App Services部署设置的方式进行配置?在MVC应用程序初始化中是否存在应该执行此操作的某个事件,或者几乎可以在任何时候执行此操作?我也在使用Trace Listener集成.
默认情况下,在ApplicationInsights.config文件中设置Instrumentation Key(iKey).此外,如果包含JavaScript部分,则再次在_Layout.cshtml文件中设置iKey .这是两个不同的地方,你需要管理一个iKey.
我希望能够通过Azure门户的应用服务 - > 应用程序设置选项卡管理此密钥.原因是:
这是我目前正在使用的实现,它似乎工作.但是,我有其他实现似乎设置iKey太早或太晚,因为它似乎在web.config部署到Azure 的物理文件中使用iKey 而不是从Portal中的应用程序设置选项卡中提取.是否有更好的选择以最佳实践方式执行此操作?
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>
Run Code Online (Sandbox Code Playgroud)
的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();
}
Run Code Online (Sandbox Code Playgroud)
web.config中
<!-- Add the following to <appSettings> and put your iKey value in here. -->
<add key="ai:InstrumentationKey" value="*****" />
Run Code Online (Sandbox Code Playgroud)
_Layout.cshtml(在<head>HTML 的部分.注意未来的读者:我建议你不要使用这整个片段,而只是使用开始的行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>
Run Code Online (Sandbox Code Playgroud)
您指定的所有方法都很棒。我们的建议是使用 web.config 应用程序设置,并在 global.asax.cs 中使用它进行标准初始化。当我们连接到 OnBeginRequest() 时,在初始化之前不会发送任何遥测数据。
另一种可能有效的方法是设置APPINSIGHTS_INSTRUMENTATIONKEYSDK 获取的环境变量。当然,这取决于您是否在同一台计算机上有多个应用程序。
| 归档时间: |
|
| 查看次数: |
2919 次 |
| 最近记录: |