Basic MSMQ Dead Letter Queue Monitoring With Custom Checks

Component: MSMQ Transport
NuGet Package NServiceBus (5.x)

When using MSMQ, the delivery of a message may fail. That can be for a number of reasons including network failures, a deleted queue, a full queue, authentication failure, or a failure to deliver on time. In these cases, the message will be moved to the MSMQ Dead Letter Queue (DLQ).

Since these messages can contain valuable business data, it is important to monitor the dead letter queue. Also, if the dead letter queue fills up, it can cause MSMQ to run out of resources and the system as a whole to fail.

This sample shows how to use a Custom Check to monitor the dead letter queue. The custom check runs once every minute. If there are no messages in the dead letter queue then the custom check reports success. Otherwise, the custom check reports failure and includes the number of messages in the DLQ.

This is what the CustomCheck will look like in ServicePulse.

CustomCheck reported in ServicePulse

Prerequisites

  • Ensure that MSMQ has been installed.
  • Ensure that ServiceControl and ServicePulse have been installed.

Code walk-through

Configure the custom checks plugin with the location of the ServiceControl input queue.

<appSettings>
  <add key="ServiceControl/Queue"
       value="particular.servicecontrol" />
</appSettings>

Check Dead Letter Queue Length

class CheckDeadLetterQueue :
    PeriodicCheck
{
    PerformanceCounter dlqPerformanceCounter;

    public CheckDeadLetterQueue() :
        base("Dead Letter Queue", "Transport", TimeSpan.FromMinutes(1))
    {
        dlqPerformanceCounter = new PerformanceCounter(
            categoryName: "MSMQ Queue",
            counterName: "Messages in Queue",
            instanceName: "Computer Queues",
            readOnly: true);
    }

    public override CheckResult PerformCheck()
    {
        var currentValue = dlqPerformanceCounter.NextValue();

        if (currentValue <= 0)
        {
            return CheckResult.Pass;
        }
        return CheckResult.Failed($"{currentValue} messages in the Dead Letter Queue on {Environment.MachineName}");
    }
}

Related Articles

  • CustomChecks Plugin
    Define a custom set of conditions that are checked on the endpoint.
  • MSMQ Dead Letter Queues
    Controlling MSMQ Dead Letter Queue behavior.
  • MSMQ Transport
    MSMQ is the primary durable communications technology for Microsoft but does not dynamically detect network interfaces.

Last modified