我正在使用 Azure Functions:大多数情况下,我尝试将现有的网络作业迁移到 Azure Functions,现在是时候将 Application Insights 集成到我的一个函数中了。
所以基本上我只需要一个实例,TelemetryClient
但这假设我能够在应用程序停止时刷新内存缓冲区。
我使用了 TimerTrigger 但它只是为了测试目的。
我引用了Microsoft.ApplicationInsights nuget 包(来自此 SO 帖子),我的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。
为了模拟应用程序关闭,我"test"
在底层 Web 应用程序中创建了一个应用程序设置,并在我希望主机重新启动时对其进行了修改:
不幸的是,这不起作用......我没有"TelemetryClientFlush"
从应用洞察仪表板中看到任何带有名称的事件:
所以我现在想知道当天蓝色功能主机停止时是否有任何方法可以拦截?