Consume raw data from Metrics

Component: Metrics
Target Version: NServiceBus 6.x
Standard support for version 6.x of NServiceBus has expired. For more information see our Support Policy.

When Performance Counters reporting and ServiceControl reporting is not enough, it's possible to consume raw metrics data by directly attaching to the public API provided by the package. First, the Metrics themselves need to be enabled. Then, a custom reporter can be attached to send data to any collector e.g. Service Control, Azure Application Insights, etc.

Enabling NServiceBus.Metrics

var metrics = endpointConfiguration.EnableMetrics();

Reporting metrics data

Metrics can be reported in a few different ways.

To any external storage

Custom observers might be registered to access every value reported by probes.

var durationsLog = LogManager.GetLogger("Durations");
var signalsLog = LogManager.GetLogger("Signals");

    register: context =>
        foreach (var duration in context.Durations)
                observer: length =>
                    durationsLog.DebugFormat("{0} = {1}", duration.Name, length);
        foreach (var signal in context.Signals)
                observer: () =>
                    signalsLog.DebugFormat("{0}", signal.Name);
Methods provided below that enable logging metrics data are obsoleted in version 1.1 and will be removed in the next major version.

To NServiceBus log

Metrics data can be written to the NServiceBus Log.

metrics.EnableLogTracing(interval: TimeSpan.FromMinutes(5));
By default metrics will be written to the log at the DEBUG log level. The API allows this parameter to be customized.
    interval: TimeSpan.FromMinutes(5),
    logLevel: LogLevel.Info);

To trace log

Metrics data can be written to System.Diagnostics.Trace.

metrics.EnableMetricTracing(interval: TimeSpan.FromSeconds(5));

To custom function

Metrics data can be consumed by a custom function.

    func: data =>
        // process metrics
        return Task.CompletedTask;
    interval: TimeSpan.FromSeconds(5));


Last modified