DalhousieDuck
DalhousieDuck

Reputation: 339

How can I log a message in an azure function after the built-in retry policy fails?

I am writing an azure function to send messages to a service bus, which is below. According to the documentation, there is a built-in retry policy that will retry 5 times if the service bus returns a transient exception. I want to write a single log statement in the event of either of these two scenarios:

  1. a non-transient error occurs
  2. a transient error occurs, but all retries from the retry policy failed.

In the case of a transient exception, will my log statement execute for each retry done by the retry policy? Or does the retry policy try five times and then re-throw the exception, so that my custom log statement only executes once after the retries?

[FunctionName("MessageLoader")]
[return: ServiceBus("%Topic_Name%", Connection = "Topic_Connection")]
public static async Task<TopicMessage> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "domain/load-message")] HttpRequestMessage req)
{
    try
    {
        var stringRequestBody = await req.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<TopicMessage>(stringRequestBody);
    }
    catch
    {
        _log.LogError("Could not send message: " + customBusinessObject.custom_message"); }
    }
}

Upvotes: 1

Views: 425

Answers (1)

Sergey Solianyk
Sergey Solianyk

Reputation: 391

Your code will log an error for each retry.

Message that couldn't be processed (all retries were failed) will be moved to the dead-letter queue.

So, if you want to perform some workload after all retires were failed, you could create separate function and subscribe it to dead-letter queue. Path to the dead-letter queue looks like:

<queue path>/$deadletterqueue
<topic path>/Subscriptions/<subscription path>/$deadletterqueue

Please, read more info here: https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dead-letter-queues#path-to-the-dead-letter-queue

Upvotes: 1

Related Questions