title: Azure Webjobs ServiceBusTrigger - perform a different function when MaxDeliveryCount is reached
body: <p>I have several WebJobs with ServiceBus triggers, and I have a custom <a href="https://github.com/Azure/azure-webjobs-sdk/blob/dev/src/Microsoft.Azure.WebJobs.ServiceBus/MessageProcessor.cs" rel="nofollow noreferrer">MessageProcessor</a> to do some processing after the message has finished processing.</p>
  
  <p>I'd like to be able to do something different (specifically, log an Error instead of a Warning) if the message is on its last retry, i.e. it's just about to be sent to the deadletter queue. The BrokeredMessage sent into the CompleteProcessingMessageAsync function has the DeliveryCount, but I can't see a way to get back to the original queue to find the MaxDeliveryCount. Any ideas? Different queues have different MaxDeliveryCounts, so setting a constant isn't really an option. The only other thing I can think of is to create a separate job for each queue's dead letter queue, but I'd like to be able to do it at the WebJob level rather than for each individual job.</p>
  
  <pre><code>public class CustomMessageProcessor : MessageProcessor
  {
      public CustomMessageProcessor(OnMessageOptions messageOptions) : base(messageOptions)
      {
      }
  
      public override async Task CompleteProcessingMessageAsync(BrokeredMessage message, FunctionResult result, CancellationToken cancellationToken)
      {
          if (result.Succeeded)
          {
              if (!MessageOptions.AutoComplete)
              {
                  cancellationToken.ThrowIfCancellationRequested();
                  await message.CompleteAsync();
              }
          }
          else
          {
              cancellationToken.ThrowIfCancellationRequested();
  
              //some other processing
  
              //If message.DeliveryCount &lt; maxDeliveryCount 
              //  log warning
              //else
              //  log error
  
              await message.AbandonAsync();
          }
      }
  }
  </code></pre>