1
votes

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?

2
can you please add some code? and let us know which version of webjobs nuget package you're using.Ivan Yang
Please find the updated with the code and details about the issuessindhuja

2 Answers

0
votes

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.

0
votes

After inspecting the source code of the Application Insights SDK it became apparent that to get an Exception in Application Insights you must pass an exception object into the LogError call.

log.Error(ex, "my error message") - will result in Application Insight Exception

log.Error("my error message") - will result in Application Insight Trace.

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

You could set config.Queues.MaxDequeueCount = 1; in webjob. The number of times to try processing a message before moving it to the poison queue.

And where is the MaxDequeueCount configuration should be added in the code?

You could set the property in JobHostConfiguration in program.cs