拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClient

Intercept Azure Function Host Shutdown: Flush Application Insights TelemetryClient(拦截 Azure Function 主机关闭:刷新 Application Insights TelemetryClient)
本文介绍了拦截 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)