NServiceBus.Extensions.Logging Usage

Component: NServiceBus Extensions Logging
NuGet Package NServiceBus.Extensions.Logging (1.x)
Target NServiceBus Version: 7.x

This sample shows how to configure an NServiceBus endpoint to use the Microsoft.Extensions.Logging package in combination with NLog.

Both Microsoft.Extensions.Logging and NServiceBus.Logging are logging abstractions and both must be configured.

The following logging chain is created:

  • NServiceBus.Logging
    • NServiceBus.Extensions.Logging
      • Microsoft.Extensions.Logging
        • NLog.Extensions.Logging
          • NLog
            • Console output

Configure NLog

NLog in this example is configured in code:

var config = new LoggingConfiguration();

var consoleTarget = new ColoredConsoleTarget
{
    Layout = "${level}|${logger}|${message}${onexception:${newline}${exception:format=tostring}}"
};
config.AddTarget("console", consoleTarget);
config.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Debug, consoleTarget));

NLog.LogManager.Configuration = config;
There is no preference on how NLog is configured. Based on the NLog documentation the most used method is with an NLog configuration file.
It is important that the NLog, Microsoft.Extensions.Logging, and NServiceBus.Logging abstractions are initialized before Endpoint.Create or Endpoint.Start is invoked. If logging is not fully initialized it is not guaranteed that logging will work as expected.

Configure logging abstractions

The following snippet shows how to initialize logging. NLog has its own provider extensions for Microsoft.Extensions.Logging and needs an NLogLoggerFactory provider that implements Microsoft.Extensions.Logging.ILoggerFactory instance so that Microsoft.Extensions.Logging can use NLog.

Microsoft.Extensions.Logging.ILoggerFactory extensionsLoggerFactory = new NLogLoggerFactory();

NServiceBus.Logging.ILoggerFactory nservicebusLoggerFactory = new ExtensionsLoggerFactory(loggerFactory: extensionsLoggerFactory);

NServiceBus.Logging.LogManager.UseFactory(loggerFactory:nservicebusLoggerFactory);

After logging initialization endpoints can be started or created.

Related Articles


Last modified