簡體   English   中英

如何在Application Insights中跟蹤來自Azure WebJobs的自定義事件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM