<legend id='mBBHx'><style id='mBBHx'><dir id='mBBHx'><q id='mBBHx'></q></dir></style></legend><tfoot id='mBBHx'></tfoot>
    1. <i id='mBBHx'><tr id='mBBHx'><dt id='mBBHx'><q id='mBBHx'><span id='mBBHx'><b id='mBBHx'><form id='mBBHx'><ins id='mBBHx'></ins><ul id='mBBHx'></ul><sub id='mBBHx'></sub></form><legend id='mBBHx'></legend><bdo id='mBBHx'><pre id='mBBHx'><center id='mBBHx'></center></pre></bdo></b><th id='mBBHx'></th></span></q></dt></tr></i><div id='mBBHx'><tfoot id='mBBHx'></tfoot><dl id='mBBHx'><fieldset id='mBBHx'></fieldset></dl></div>

      <small id='mBBHx'></small><noframes id='mBBHx'>

          <bdo id='mBBHx'></bdo><ul id='mBBHx'></ul>

      1. 拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClie

        时间:2023-10-05

          • <legend id='iYYVw'><style id='iYYVw'><dir id='iYYVw'><q id='iYYVw'></q></dir></style></legend>
              <tbody id='iYYVw'></tbody>

          • <tfoot id='iYYVw'></tfoot>
            • <bdo id='iYYVw'></bdo><ul id='iYYVw'></ul>
            • <i id='iYYVw'><tr id='iYYVw'><dt id='iYYVw'><q id='iYYVw'><span id='iYYVw'><b id='iYYVw'><form id='iYYVw'><ins id='iYYVw'></ins><ul id='iYYVw'></ul><sub id='iYYVw'></sub></form><legend id='iYYVw'></legend><bdo id='iYYVw'><pre id='iYYVw'><center id='iYYVw'></center></pre></bdo></b><th id='iYYVw'></th></span></q></dt></tr></i><div id='iYYVw'><tfoot id='iYYVw'></tfoot><dl id='iYYVw'><fieldset id='iYYVw'></fieldset></dl></div>

                <small id='iYYVw'></small><noframes id='iYYVw'>

                  本文介绍了拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Azure Functions:主要是尝试将现有的网络作业迁移到 Azure Functions,现在是时候将 Application Insights 集成到我的一个函数中了.

                  所以基本上我只需要 TelemetryClient 的一个实例,但这假设我能够在应用程序停止时刷新内存缓冲区.

                  我使用了 TimerTrigger,但它只是用于测试目的.

                  我已经引用了

                  很遗憾,这不起作用...我没有从应用洞察仪表板中看到任何名为 "TelemetryClientFlush" 的事件:

                  所以我现在想知道当azure函数主机停止时是否有任何方法可以拦截?

                  解决方案

                  除了 Mathew 所描述的之外,您可能还想玩弄我们将在请求时传递的取消令牌.

                  如果您向函数添加 CancellationToken 类型参数,我们将传入一个令牌,该令牌将在主机正常关闭时发出信号.使用它可能会让你接近你需要什么:

                  使用系统;使用 System.Threading;使用 Microsoft.ApplicationInsights;公共静态只读 TelemetryClient TelemetryClient = new TelemetryClient(){ InstrumentationKey = "MyInstrumentationKey" };公共静态布尔优先=真;公共静态无效运行(TimerInfo myTimer,TraceWriter 日志,CancellationToken 令牌){如果(第一){token.Register(() =>{TelemetryClient.TrackEvent("TelemetryClientFlush");TelemetryClient.Flush();});第一=假;}TelemetryClient.TrackEvent("AzureFunctionTriggered");log.Verbose($"C# Timer 触发函数执行于:{DateTime.Now}");}

                  I am playing a little bit with Azure Function: Mostly I try to migrate an existing webjob to Azure Functions and now it is time for me to integrate Application Insights in one of my function.

                  So basically I only need one instance of the TelemetryClient but this assumes that I am able to flush the in-memory buffer when the application stops.

                  I've used a TimerTrigger but it was just for testing purpose.

                  I've referenced the Microsoft.ApplicationInsights nuget package (from this SO post) and my run.csx file looks like that:

                  using System;
                  using Microsoft.ApplicationInsights;
                  using Microsoft.Azure.WebJobs;
                  
                  public static void Run(TimerInfo myTimer, TraceWriter log)
                  {
                      MyTimerJob.TelemetryClient.TrackEvent("AzureFunctionTriggered");
                      log.Verbose($"C# Timer trigger function executed at: {DateTime.Now}");
                  }    
                  
                  public static class MyTimerJob
                  {
                      public static readonly TelemetryClient TelemetryClient;
                  
                      static MyTimerJob(){
                          TelemetryClient = new TelemetryClient()
                              { InstrumentationKey = "MyInstrumentationKey" };
                  
                          // When it shutdowns, we flush the telemty client.
                          new WebJobsShutdownWatcher().Token.Register(() =>
                          {
                              TelemetryClient.TrackEvent("TelemetryClientFlush");
                              TelemetryClient.Flush();
                          });
                      }
                  }
                  

                  This implementation is a bit tricky...

                  • I have a static TelemetryClient to ensure that I am going to reuse the same instance.
                  • I tried using the WebJobsShutdownWatcher to detect when the host is stopping so that I can flush the TelemetryClient.

                  To simulate the application shutdown, I've created a "test" app setting in the underlying web app and I modify it when I want the host to restart:

                  Unfortunately this does not work... I did not see any event with name "TelemetryClientFlush" from the app insights dashboard:

                  So I am now wondering if there is any way to intercept when the azure function host is stopping ?

                  解决方案

                  In addition to what Mathew described, you may want to play around with the cancellation token we'll pass if requested.

                  If you add a CancellationToken type argument to your function, we'll pass in a token that will be signaled when the host shuts down under normal circumstances. Using that may get you close to what you need:

                  using System;
                  using System.Threading;
                  using Microsoft.ApplicationInsights;
                  
                  public static readonly TelemetryClient TelemetryClient = new  TelemetryClient(){ InstrumentationKey = "MyInstrumentationKey" };
                  public static bool first = true;
                  public static void Run(TimerInfo myTimer, TraceWriter log, CancellationToken token)
                  {
                      if(first){
                          token.Register(() =>
                          {
                              TelemetryClient.TrackEvent("TelemetryClientFlush");
                              TelemetryClient.Flush();
                          });
                          first = false;
                      }
                  
                      TelemetryClient.TrackEvent("AzureFunctionTriggered");
                      log.Verbose($"C# Timer trigger function executed at: {DateTime.Now}");
                  }
                  

                  这篇关于拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在我的 Azure Function 中引用 c# 类库 下一篇:Azure 函数静态隔离

                  相关文章

                    <bdo id='6IXwl'></bdo><ul id='6IXwl'></ul>
                • <legend id='6IXwl'><style id='6IXwl'><dir id='6IXwl'><q id='6IXwl'></q></dir></style></legend>

                    <small id='6IXwl'></small><noframes id='6IXwl'>

                    <tfoot id='6IXwl'></tfoot>
                    <i id='6IXwl'><tr id='6IXwl'><dt id='6IXwl'><q id='6IXwl'><span id='6IXwl'><b id='6IXwl'><form id='6IXwl'><ins id='6IXwl'></ins><ul id='6IXwl'></ul><sub id='6IXwl'></sub></form><legend id='6IXwl'></legend><bdo id='6IXwl'><pre id='6IXwl'><center id='6IXwl'></center></pre></bdo></b><th id='6IXwl'></th></span></q></dt></tr></i><div id='6IXwl'><tfoot id='6IXwl'></tfoot><dl id='6IXwl'><fieldset id='6IXwl'></fieldset></dl></div>