Home > Software design >  How to read deadletter count from service bus topic and its subscription in c# using .NET SDK for Az
How to read deadletter count from service bus topic and its subscription in c# using .NET SDK for Az

Time:09-16

Azure Monitor provides many metrics for Service Bus such as “Incoming, Outgoing, Active” Messages Service bus Matrics giving you the total message count and when trying to look into the details of that metric “DeadletteredMessages”, I can get counts by the “EntityName” which is either a queue or a topic but not the subscription-level metrics.

I was facing challenges while getting all the dead-letter and other messages count from the topic and its subscriptions. I have spent almost 4-5 hrs to resolve the above issue.

I hope, below answer might be a help to Monitor service Bus

CodePudding user response:

Here is the sample code for the same.

[FunctionName("ServiceBusMonitor")]
        public async Task Run([TimerTrigger("0 */1 * * * *")] TimerInfo myTimer, ILogger log)
        {
            try
            {
                var client = new ServiceBusAdministrationClient(_configuration.GetValue<string>("ServiceBusConnectionString"));
                var topics = client.GetTopicsAsync()?.GetAsyncEnumerator();
                while (await topics.MoveNextAsync())
                {
                    TopicProperties topicProperties = topics.Current;
                    var subscriptions = client.GetSubscriptionsAsync(topicProperties.Name)?.GetAsyncEnumerator();
                    log.LogInformation($"{topicProperties.Name}");

                    var subscriptionRuntimePropertiesAsync = client.GetSubscriptionsRuntimePropertiesAsync(topicProperties.Name)?.GetAsyncEnumerator();
                    while (await subscriptionRuntimePropertiesAsync.MoveNextAsync())
                    {
                        var subscriptionRuntimeProperties = subscriptionRuntimePropertiesAsync.Current;
                        Console.WriteLine($"Topic Name {subscriptionRuntimeProperties.TopicName}, Subscription Name {subscriptionRuntimeProperties.SubscriptionName}");
                        Console.WriteLine($"DeadLetterMessageCount : {subscriptionRuntimeProperties.DeadLetterMessageCount} ActiveMessageCount : {subscriptionRuntimeProperties.ActiveMessageCount} TotalMessageCount : {subscriptionRuntimeProperties.TotalMessageCount} " );
//You can log subscriptionRuntimeProperties in Application insight and use the for the monitor
                    }
                    Console.WriteLine("-----------");
                }

                log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            }
            catch (Exception ex)
            {
                log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now} {ex.Message}");

            }
        }

CodePudding user response:

Talking only about monitoring Service Bus there is a great tool out there called Service Bus Explorer:

The Service Bus Explorer allows users to efficiently administer messaging entities. The tool provides advanced features like import/export functionality and the ability to test topic, queues, subscriptions, relay services, notification hubs and events hubs.

Some of this tool's functionality is also available (in preview) via the Azure Portal when you select a specific Queue or Topic:

The Service Bus Explorer tool on the Azure portal is now available in preview.

Azure Service Bus, like most other PaaS offerings, has two sets of operations that can be performed against it:

Management operations like CRUD (create, read, update, and delete) on Service Bus namespaces, queues, topics, subscriptions, and filters. Data operations like send, receive, and peek on queues, topics, and subscriptions.

Both approaches will let you manually keep track of your topics and queues.

  • Related