简体   繁体   English

如何在Application Insights中跟踪来自Azure WebJobs的自定义事件?

[英]How do you track custom events from Azure WebJobs in Application Insights?

I have an Azure WebJobs (v2.2.0) project that I would like to monitor with Application Insights (AI), and there are events that I would like to be able to track. 我有一个Azure WebJobs(v2.2.0)项目,我想使用Application Insights(AI)进行监视,并且我希望能够跟踪一些事件。 In a normal web app that is configured to use AI you can just use this: 在配置为使用AI的普通Web应用程序中,您可以使用以下命令:

TelemetryClient tc = new TelemetryClient();
tc.TrackEvent("EventName");

However this seems not to work in the context of a WebJob! 但是,这似乎不适用于WebJob! I have configured my WebJob project as per the instructions on the WebJob SDK repo which ends up looking like this: 我已经按照WebJob SDK存储库上的说明配置了WebJob项目,最终看起来像这样:

Program 程序

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();
      }
    } 
  }
}

Functions 职能

This is just a test function that will run every minute for half an hour. 这只是一个测试功能,它将每分钟运行半小时。

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) { }
    }
  }
}

This seems to partially work in that I can see some telemetry in AI but not the custom events. 这似乎部分起作用,因为我可以看到AI中的一些遥测,但看不到自定义事件。 For example I can see a Request show up each time TestFunctions.TelemetryTest() runs and various Trace s during the initialisation of the WebJob. 例如,在WebJob初始化期间,每次TestFunctions.TelemetryTest()运行时,我都可以看到一个Request以及各种Trace

I have probably not configured something properly or am not getting the TelemetryClient in the correct manner, but I cannot find any documentation on tracking custom events in WebJobs. 我可能没有正确配置某些东西,或者没有以正确的方式获取TelemetryClient ,但是在WebJobs中找不到有关跟踪自定义事件的任何文档。

Any help would be appreciated. 任何帮助,将不胜感激。

Try setting the instrumentationkey explicit: 尝试显式设置Instrumentation键:

tc.Context.InstrumentationKey = "<your_key>";

According to the docs you should be able to get the key using 根据文档,您应该能够使用

System.Environment.GetEnvironmentVariable(
            "APPINSIGHTS_INSTRUMENTATIONKEY", EnvironmentVariableTarget.Process)

if you have set up application insights integration. 如果您已设置应用程序见解集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM