Using EventSourceLogging

Component: EventSourceLogging
NuGet Package NServiceBus.EventSourceLogging (1.x)
This is a community maintained project
Target NServiceBus Version: 6.x


Illustrates writing log entries to event tracing for Windows (ETW) using EventSourceLogging.

Required Implementations

Several class implementations are required to define how the Event Tracing for Windows logging should function.


The implementation of EventListener gives full control over how EventWrittenEventArgs are written.

In this case only OnEventWritten is overwritten to write to the console.

OnEventWritten is called whenever an event has been written by an event source for which the event listener has enabled events.

Typically, events would not be written to the console. This is for example purposes only.

class CustomEventListener :
    protected override void OnEventWritten(EventWrittenEventArgs data)
        var message = string.Format(data.Message, data.Payload?.ToArray() ?? new object[0]);
        Console.WriteLine($"{data.EventId} {data.Channel} {data.Level} {message}");


The implementation of EventSourceLoggerBase allows custom handling of of all levels of logging, as well as assigning the EventAttribute settings that are be used for each method.

    Name = "Samples.Logging.SimpleSourceLogger",
    LocalizationResources = "Sample.NServiceBusEventSourceResources")]
public sealed class SimpleSourceLogger :
    [Event(1, Level = EventLevel.Verbose)]
    public override void Debug(string logger, string message)
        base.Debug(logger, message);

    [Event(2, Level = EventLevel.Verbose)]
    public override void DebugException(string logger, string message, string exceptionType, string exceptionMessage, string exceptionValue)
        base.DebugException(logger, message, exceptionType, exceptionMessage, exceptionValue);

Enabling Logging

Enabling EventSourceLogger involves several steps

  1. Instantiate the EventListener.
  2. Instantiate the EventSourceLoggerBase.
  3. Pass the EventSourceLoggerBase instance to the EventListener instance with an EventLevel.
  4. Configure NServiceBus to use EventSourceLogger by calling LogManager.Use<EventSourceLoggingFactory>() and then passing the EventSourceLoggerBase instance to the EventSourceLoggingFactory.

Then at service shutdown time dispose of both the instance of EventListener and the instance of EventSourceLoggerBase.

using (var listener = new CustomEventListener())
using (var eventSourceLogger = new SimpleSourceLogger())
    listener.EnableEvents(eventSourceLogger, EventLevel.Informational);
    var loggingFactory = LogManager.Use<EventSourceLoggingFactory>();

    var endpointConfiguration = new EndpointConfiguration("Samples.Logging.SimpleSourceLogger");

    var endpointInstance = await Endpoint.Start(endpointConfiguration)
    await SendMessage(endpointInstance)
    Console.WriteLine("Press any key to exit");
    await endpointInstance.Stop()

This code uses self-hosting console scenario for example purposes. Typically the EventListener would be instantiated at service startup and disposed of in service shutdown. Depending on the hosting approach it may not be possible to have a using wrap the instances. See also Hosting and Windows Service Hosting.

Related Articles

Last modified