Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

NServiceBus.Extensions.Logging Usage

NuGet Package: NServiceBus.Extensions.Logging (3.x)
Target Version: NServiceBus 9.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;

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