Using EventSourceLogging

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

Code walk-through

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

Required Implementations

Several class implementations are required to define how the EventSourceLogger should function.

EventListener

The implementation of EventListener gives full contol 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 :
    EventListener
{
    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}");
    }
}

EventSourceLoggerBase

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

[EventSource(
    Name = "Samples.Logging.SimpleSourceLogger",
    LocalizationResources = "Sample.NServiceBusEventSourceResources")]
public sealed class SimpleSourceLogger :
    EventSourceLoggerBase
{
    [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>();
    loggingFactory.WithLogger(eventSourceLogger);

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

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

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

  • Logging
    Manage and integrate with NServiceBus logging.

Last modified