简体   繁体   中英

Monitoring Azure WebJobs Health based on the errors in the WebJob logs with Application Insights

I was configured the Multi-step web test for monitoring the Azure Web Jobs health using Azure Application Insights by following this documentation . But this multi-step web test will check only the status of Azure Web Job, whether it is “Running, Failed and Aborted”.

Sometimes, Azure Web Job was Aborted. But the job runs inside it. So, I need to monitor the status of Azure Web Job based on error in the logs like shown in below figure using Multi-step web test. 在此输入图像描述

You could use Application Insights Integration to implement it. The LogCategoryFilter has a Default property with an initial value of Information, meaning that any messages with levels of Information, Warning, Error or Critical will be logged.

You need three packages in total:

  1. Microsoft.Azure.WebJobs.Logging.ApplicationInsights
  2. Microsoft.Extensions.Logging
  3. Microsoft.Extensions.Logging.Console

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

Note : Don't forget adding the APPINSIGHTS_INSTRUMENTATIONKEY in your application setting.

I test ProcessQueueMessage webjob.

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...");
    }

This is my webjob log.

在此输入图像描述

And this is the Application Insight page. You could find the Information, Warning and Exception are all shown there.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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