【问题标题】:Configurable Application Insights Instrumentation Key可配置的 Application Insights Instrumentation Key
【发布时间】:2017-07-04 13:56:47
【问题描述】:

我怎样才能最好地使应用程序 Inisghts 的 Instrumentation Key 可配置,以允许 Azure 管理员管理 MVC5 Web 应用程序的应用服务部署的设置?在 MVC 应用程序初始化中是否有某个事件应该完成,或者在几乎任何时候都可以这样做?我也在使用 Trace Listener 集成。

默认情况下,检测密钥 (iKey) 在 ApplicationInsights.config 文件中设置。此外,如果您包含 JavaScript 部分,则 iKey 会再次设置在 _Layout.cshtml 文件中。这是两个不同的地方,您需要管理一个 iKey。

我希望能够通过 Azure 门户的 App Services -> Application settings 选项卡管理此密钥。原因是:

  1. 我想部署此应用程序的多个实例,每个实例都有自己唯一的 iKey
  2. 我想定期更换这个 iKey(因为原因
  3. 我不希望这个 iKey 存储在我们的代码库中(“开发”iKey 可以放在代码库中),也不希望它由我们的构建自动化管理(再次,因为 原因)

【问题讨论】:

    标签: asp.net-mvc azure asp.net-mvc-5 azure-application-insights


    【解决方案1】:

    这是我目前正在使用的实现,它似乎可以工作。但是,我有其他实现似乎将 iKey 设置得太早或太晚,因为它似乎会使用部署到 Azure 的物理 web.config 文件中的 iKey,而不是从 应用程序设置 中提取门户中的选项卡。是否有更好的选择以最佳实践方式执行此操作?

    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 的 &lt;head&gt; 部分。未来读者注意:我建议您不要使用整个 sn-p,而只需使用以 @ 开头的行987654326@ 并将该行集成到此 JS sn-p 其余部分的现代版本中!):

    <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>
    

    【讨论】:

    • 尽管在这里提供了我自己的问题的答案,但我希望其他人可以提供更好的答案。如果你能改进它,我会接受你的答案而不是我的答案。 :-)
    【解决方案2】:

    您指定的所有方法都很棒。我们的建议是使用 web.config 应用程序设置并在 global.asax.cs 中使用它进行标准初始化。当我们挂钩到 OnBeginRequest() 时,在初始化之前不会发送任何遥测数据。

    https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#a-namedynamic-ikeya-dynamic-instrumentation-key

    另一种可行的方法是设置 APPINSIGHTS_INSTRUMENTATIONKEY 环境变量,因为它被 SDK 拾取。当然,这取决于您在同一台机器上是否有多个应用程序。

    https://github.com/Microsoft/ApplicationInsights-dotnet/blob/v2.2.0/src/Core/Managed/Net40/Extensibility/Implementation/TelemetryConfigurationFactory.cs#L22

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2020-02-22
      相关资源
      最近更新 更多