Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Custom NLog configuration

Component: NLog Logger
NuGet Package: NServiceBus.NLog (3.x)
Target Version: NServiceBus 7.x

Introduction

Illustrates customizing NLog usage by configuring NLog targets and rules.

Configure NLog

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("*", LogLevel.Info, consoleTarget));

LogManager.Configuration = config;

Pass that configuration to NServiceBus

NServiceBus.Logging.LogManager.Use<NLogFactory>();

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

Verifying that the sample works correctly

In this sample the information at the Info level is logged to the console window.

There will be a few standard logging entries in the console window that are automatically created by NServiceBus when logging level is set to Info, for example:

 Queue [private$\error] is running with [Everyone] and [NT AUTHORITY\ANONYMOUS LOGON] permissions. Consider setting appropriate permissions, if required by the organization. For more information, consult the documentation.

There will also be a custom entry logged from inside the handler Hello from MyHandler.

Related Articles