Search code examples
azureazure-webjobsazure-application-insightsazure-webjobssdk

How to handle exceptions from webjobs in application insights?


When an exception is thrown from webjob, it exits without logging to the application insights. Observed that flushing the logs to application insights takes few minutes, so we are missing the exceptions here. How to handle this?

Also, is there a way to move the message which hit the exception to poison queue automatically without manually inserting that message to poison queue?

I am using latest stable 3.x versions for the 2 NuGet packages: Microsoft.Azure.WebJobs and Microsoft.Azure.WebJobs.Extensions

Created a host that implemented IHost as below:

        var builder = new HostBuilder()
            .UseEnvironment("Development")
            .ConfigureWebJobs(b =>
            {
                ...
            })
            .ConfigureLogging((context, b) =>
            {
                string appInsightsKey = context.Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
                if (!string.IsNullOrEmpty(appInsightsKey))
                {
                    b.AddApplicationInsights(o => o.InstrumentationKey = appInsightsKey);
                    appInsights.TrackEvent("Application Insights is starting!!");
                }
            })
            .ConfigureServices(services =>
            {
              ….
            })
            .UseConsoleLifetime();
        var host = builder.Build();
        using (host)
        {
            host.RunAsync().Wait();
        }

and Function.cs

   public static async void ProcessQueueMessageAsync([QueueTrigger("queue")] Message message, int dequeueCount, IBinder binder, ILogger logger)
    {
        switch (message.Name)
        {
            case blah:
                ...break;
            default:
                logger.LogError("Invalid Message object in the queue.", message);
                logger.LogWarning("Current dequeue count: " + dequeueCount);
                throw new InvalidOperationException("Simulated Failure");
        }
    }

My questions here are:

1) When the default case is hit, webjob is terminating immediately and the loggers are not getting flushed into app insights even after waiting and starting the web job again. As it takes few minutes to reflect in app insights, and webjob stops, I am losing the error logs. How to handle this?

2) From the sample webjobs here, https://github.com/Azure/azure-webjobs-sdk-samples/blob/master/BasicSamples/QueueOperations/Functions.cs they are using JobHost host = new JobHost(); and if the 'FailAlways' function fails, it automatically retries for 5 times and pushed the message into poison queue. But this is not happening in my code. Is it because of different Hosts? or do I have to add any more configurations?


Solution

  • Try changing your function to return Task instead of void:

    public static async Task ProcessQueueMessageAsync([QueueTrigger("queue")] Message message, int dequeueCount, IBinder binder, ILogger logger)
    

    This worked for me where even though I was logging the error and throwing the exception, Application Insights would either show a successful invocation or no invocation occurring.