[英]How do you track custom events from Azure WebJobs in Application Insights?
我有一个Azure WebJobs(v2.2.0)项目,我想使用Application Insights(AI)进行监视,并且我希望能够跟踪一些事件。 在配置为使用AI的普通Web应用程序中,您可以使用以下命令:
TelemetryClient tc = new TelemetryClient();
tc.TrackEvent("EventName");
但是,这似乎不适用于WebJob! 我已经按照WebJob SDK存储库上的说明配置了WebJob项目,最终看起来像这样:
using System.Configuration;
using System.Diagnostics;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace WebJobs
{
public class Program
{
public static void Main()
{
JobHostConfiguration config = new JobHostConfiguration();
config.UseTimers();
using (LoggerFactory loggerFactory = new LoggerFactory())
{
string key = ConfigurationManager.AppSettings["webjob-instrumentation-key"];
loggerFactory.AddApplicationInsights(key, null);
loggerFactory.AddConsole();
config.LoggerFactory = loggerFactory;
config.Tracing.ConsoleLevel = TraceLevel.Off;
if (config.IsDevelopment)
config.UseDevelopmentSettings();
JobHost host = new JobHost(config);
host.RunAndBlock();
}
}
}
}
这只是一个测试功能,它将每分钟运行半小时。
using Core.Telemetry;
using Microsoft.ApplicationInsights;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Timers;
using System;
using System.Collections.Generic;
namespace WebJobs.Functions
{
public class TestFunctions
{
public void TelemetryTest([TimerTrigger(typeof(Schedule))] TimerInfo timer)
{
TelemetryClient tc = new TelemetryClient();
tc.TrackEvent("TelemetryTestEvent");
}
// schedule that will run every minute
public class Schedule : DailySchedule
{
private static readonly string[] times =
{
"12:01","12:02","12:03","12:04","12:05","12:06","12:07","12:08","12:09","12:10",
"12:11","12:12","12:13","12:14","12:15","12:16","12:17","12:18","12:19","12:20",
"12:21","12:22","12:23","12:24","12:25","12:26","12:27","12:28","12:29","12:30"
};
public Schedule() : base(times) { }
}
}
}
这似乎部分起作用,因为我可以看到AI中的一些遥测,但看不到自定义事件。 例如,在WebJob初始化期间,每次TestFunctions.TelemetryTest()
运行时,我都可以看到一个Request以及各种Trace 。
我可能没有正确配置某些东西,或者没有以正确的方式获取TelemetryClient
,但是在WebJobs中找不到有关跟踪自定义事件的任何文档。
任何帮助,将不胜感激。
尝试显式设置Instrumentation键:
tc.Context.InstrumentationKey = "<your_key>";
根据文档,您应该能够使用
System.Environment.GetEnvironmentVariable(
"APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process)
如果您已设置应用程序见解集成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.