Monitor with ServiceControl events

This sample shows how to monitor heartbeat and failed message events in ServiceControl.

Prerequisites

  1. Install ServiceControl.
  2. Using ServiceControl Management tool, set up ServiceControl to monitor endpoints using MSMQ transport.
  3. Ensure the ServiceControl process is running before running the sample.
When using the AzureServiceBus transport, it is necessary to configure the ValidateAndHashIfNeeded sanitization strategy for both endpoints in this project. This ensures that a consistent entity name-shortening strategy is used between the endpoints and ServiceControl. The same applies when using the Azure Service Bus .NET Standard transport, in which case the name-shortening rules can be specified via configuration settings.
ServiceControl endpoint queue name configured in EndpointsMonitor needs to match the name of the ServiceControl instance with the underlying transport naming rules.

Running the project

The project handles two kinds of events:

MessageFailed event

A MessageFailed event is emitted when processing a message fails and the message is moved to the error queue.

To observe this in action, press Enter in the NServiceBusEndpoint console window to send a new SimpleMessage event. Processing of the message fails every time.

When a MessageFailed event is received, the EndpointsMonitor prints the following message in its console window:

Received ServiceControl 'MessageFailed' event for a SimpleMessage.

HeartbeatStopped and HeartbeatRestored events

The HeartbeatStopped event is emitted whenever an endpoint fails to send a control message at an expected interval. The HeartbeatRestored event is emitted whenever the endpoint successfully sends a control message again.

The monitor must receive at least one control message before it can observe that the endpoint stopped responding.

To observe this in action, stop the NServiceBusEndpoint application and wait up to 30 seconds. When a HeartbeatStopped event is received, the EndpointsMonitor prints the following message in its console window:

Heartbeat from NServiceBusEndpoint stopped.

Next, restart the NServiceBusEndpoint application and wait up to 30 seconds. When a HeartbeatRestored event is received, the EndpointsMonitor prints the following message in its console window:

Heartbeat from EndpointsMonitoring.NServiceBusEndpoint restored.

Code walk-through

The solution consists of two projects: NServicebusEndpoint and EndpointsMonitor. NServiceBusEndpoint is a simple endpoint which is monitored by the EndpointsMonitor.

NServiceBusEndpoint

Retries are disabled in the sample for simplicity; messages are immediately moved to the error queue after a processing failure:

var recoverability = endpointConfiguration.Recoverability();

recoverability.Delayed(
    customizations: retriesSettings =>
    {
        retriesSettings.NumberOfRetries(0);
    });
recoverability.Immediate(
    customizations: retriesSettings =>
    {
        retriesSettings.NumberOfRetries(0);
    });

The MessageFailed event is published for any standard NServiceBus endpoint that is monitored by ServiceControl.

In order to receive HeartbeatStopped and HeartbeatRestored events, the endpoint must use the heartbeats plugin.

Heartbeat control messages are sent every 30 seconds by default so there will be up to a 30 second delay before ServiceControl realizes that it lost or restored connection with the endpoint.

EndpointsMonitor

In order to get notifications when the exposed ServiceControl events occur, create an NServiceBus endpoint. Next, reference the ServiceControl.Contracts NuGet package and implement a handler which handles ServiceControl events:

public class CustomEventsHandler :
    IHandleMessages<MessageFailed>,
    IHandleMessages<HeartbeatStopped>,
    IHandleMessages<HeartbeatRestored>
{
    static ILog log = LogManager.GetLogger<CustomEventsHandler>();

    public Task Handle(MessageFailed message, IMessageHandlerContext context)
    {
        log.Error("Received ServiceControl 'MessageFailed' event for a SimpleMessage.");
        return Task.CompletedTask;
    }

    public Task Handle(HeartbeatStopped message, IMessageHandlerContext context)
    {
        log.Warn($"Heartbeat from {message.EndpointName} stopped.");
        return Task.CompletedTask;
    }

    public Task Handle(HeartbeatRestored message, IMessageHandlerContext context)
    {
        log.Info($"Heartbeat from {message.EndpointName} restored.");
        return Task.CompletedTask;
    }
}

Samples

Related Articles


Last modified