首页 > 解决方案 > 使用 Application Insights 根据 WebJob 日志中的错误监控 Azure WebJobs 运行状况

问题描述

按照此文档,我配置了多步 Web 测试,以使用 Azure Application Insights 监控 Azure Web 作业运行状况。但是这个多步骤的 Web 测试只会检查 Azure Web Job 的状态,是否是“正在运行、失败和中止”。

有时,Azure Web 作业被中止。但工作在其中运行。因此,我需要使用 Multi-step web test 根据日志中的错误监控 Azure Web Job 的状态,如下图所示。 在此处输入图像描述

标签: azureazure-webjobsazure-application-insightsazure-webjobs-triggered

解决方案


你可以Application Insights Integration用来实现它。LogCategoryFilter 具有初始值为 Information 的 Default 属性,这意味着将记录任何具有 Information、Warning、Error 或 Critical 级别的消息。

你总共需要三个包:

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

注意:不要忘记APPINSIGHTS_INSTRUMENTATIONKEY在您的应用程序设置中添加。

我测试ProcessQueueMessage网络作业。

public static void ProcessQueueMessage([QueueTrigger("myqueue")] string message, ILogger logger)
    {
        logger.LogInformation("it is a error......");
        logger.LogError(new Exception(), "it is a test error...");
    }

这是我的网络作业日志。

在此处输入图像描述

这是 Application Insight 页面。您会发现信息、警告和异常都显示在那里。

在此处输入图像描述


推荐阅读