将Azure Application Insights与Azure WebJob一起使用

Azure文档涵盖了将Azure Application Insights集成到不同应用程序类型(如ASP.NET,Java等)的许多示例。但是,该文档未显示将Application Insights集成到Azure WebJob中的任何示例。

有没有人链接到一个示例或文章,其中包含如何将Azure Application Insights集成到作为控制台应用程序构建的Azure WebJob中?

我编写了一个控制台应用程序,通过Application Insights跟踪事件和指标,我认为通过添加以下NuGet包,WebJob将不会完全不同:

  • Microsoft.ApplicationInsights
  • Microsoft.ApplicationInsights.TraceListener(可能不需要)

我的ApplicationInsights.config看起来像这样:

      

简单的程序就是这样做的:

 TelemetryConfiguration.Active.InstrumentationKey = "the_key"; TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true; var tc = new TelemetryClient(); tc.TrackRequest("Track Some Request", DateTimeOffset.UtcNow, new TimeSpan(0, 0, 3), "200", true); tc.TrackMetric("XYZ Metric", 100); tc.TrackEvent("Tracked Event"); tc.Flush(); //need to do this, otherwise if the app exits the telemetry data won't be sent 

还有: Windows桌面应用程序,服务和工作者角色的Application Insights

由于上述答案是2年,从那时起许多事情发生了变化。 现在有一个nuget包可用于与Azure Webjobs集成Application Insight。 您需要安装以下包:

  1. Microsoft.Azure.WebJobs.Logging.ApplicationInsights(目前处于测试阶段)
  2. Microsoft.Extensions.Logging
  3. Microsoft.Extensions.Logging.Console

配置JobHostConfiguration如下:

 string instrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY"); if (!string.IsNullOrEmpty(instrumentationKey)) { // build up a LoggerFactory with ApplicationInsights and a Console Logger config.LoggerFactory = new LoggerFactory().AddApplicationInsights(instrumentationKey, null).AddConsole(); config.Tracing.ConsoleLevel = TraceLevel.Off; } 

在这里查看完整的post