【发布时间】:2016-08-14 02:27:17
【问题描述】:
我正在使用 Azure Functions:主要是尝试将现有的网络作业迁移到 Azure Functions,现在是时候将 Application Insights 集成到我的一个函数中了。
所以基本上我只需要TelemetryClient 的一个实例,但这假设我能够在应用程序停止时刷新内存缓冲区。
我使用了 TimerTrigger,但它只是用于测试目的。
我引用了 Microsoft.ApplicationInsights nuget 包 (from this SO post),我的 run.csx 文件如下所示:
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();
});
}
}
这个实现有点棘手……
- 我有一个静态的
TelemetryClient以确保我将重用同一个实例。 - 我尝试使用
WebJobsShutdownWatcher来检测主机何时停止,以便我可以刷新 TelemetryClient。
为了模拟应用程序关闭,我在底层 Web 应用程序中创建了一个 "test" 应用程序设置,并在我希望主机重新启动时对其进行修改:
很遗憾,这不起作用...我没有从应用洞察信息中心看到任何名称为 "TelemetryClientFlush" 的事件:
所以我现在想知道当azure函数主机停止时是否有任何方法可以拦截?
【问题讨论】:
-
您能否将 $log 语句添加到您的关闭回调中以确认代码完全运行。查看 WebJobsShutdownWatcher 源代码,需要遵循几个条件才能运行关闭回调:github.com/Azure/azure-webjobs-sdk/blob/master/src/…
标签: c# azure-webjobs azure-application-insights azure-webjobssdk azure-functions