Skip to content

Instantly share code, notes, and snippets.

Last active October 28, 2018 18:06
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Code Sample STTS - MonitorDeadLetterQueues
using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.ServiceBus.Management;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
namespace MonitorDeadLetterQueue
public static class MonitorDeadLetterQueues
private static readonly TelemetryClient TelemetryClient = new TelemetryClient();
private static string key = TelemetryConfiguration.Active.InstrumentationKey
= Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY",
public static async Task Run([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
var serviceBusConnectionString = Environment.GetEnvironmentVariable("MonitorServiceBusQueues");
var managementClient = new ManagementClient(serviceBusConnectionString);
var queues = await managementClient.GetQueuesAsync();
foreach (var queue in queues)
var queueName = queue.Path;
var queueRuntimeInfo = await managementClient.GetQueueRuntimeInfoAsync(queueName);
var deadLetterMessagesCount = queueRuntimeInfo.MessageCountDetails.DeadLetterMessageCount;
TelemetryClient.TrackMetric($"DeadLetterQueue length - {queueName}", deadLetterMessagesCount);
log.LogInformation($"Queue: {queueName} (DeadLetters: {deadLetterMessagesCount})");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment