将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。 您需要安装以下包:
- Microsoft.Azure.WebJobs.Logging.ApplicationInsights(目前处于测试阶段)
- Microsoft.Extensions.Logging
- 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
- 不接受Azurefunction(ILogger或TraceWriter)的上次日志记录参数
- 来自MemoryStream的AddAttachment
- 将Application Insights与unit testing结合使用?
- Azure的EventData.PartitionKey如何决定写入哪个分区?
- 无法将REST API用于Windows Server 1.1的Service Bus(OnPremises)
- 如何从Azure webjob与Azure webapp进行通信?
- Azure webrole循环和“无法加载文件或程序集System.Web.Http 5.2.0.0”
- C#Web应用程序未通过Azure混合连接连接到本地SQL Server
- 持久保存到Azure表存储时使用POCO