Monitor with ServiceControl events

Prerequisistes

  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.

Running the project

The project presents how to handle two kinds of events:

MessageFailed event

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

In order to observe that, press Enter in the NServiceBusEndpoint console window. That will send a new SimpleMessage. 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 expected interval. The HeartbeatRestored event is emitted whenever the endpoint successfully sends a control message again.

The Monitor needs to receive at least one control message, before it can observe that endpoint stopped responding.

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

Heartbeat from NServiceBusEndpoint stopped.

Then restart the NServiceBusEndpoint application and wait for 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. The NServiceBusEndpoint is a simple endpoint which is monitored by the EndpointsMonitor.

NServiceBusEndpoint

The retries are disabled in the sample for simplicity, therefore the message is immediately moved to the error queue after 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 needs to use the heartbeats plugin.

Heartbeat control messages are sent every 30 seconds by default. That means there might be up to 30 seconds 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. Then 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