Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Custom Log4Net appender

Component: Log4Net Logger
NuGet Package: NServiceBus.Log4Net (3.x)
Target Version: NServiceBus 7.x
This sample is obsolete and incompatible with NServiceBus 8+. Migration to a modern logging framework is recommended. If Log4Net must be used with NServiceBus, migrate to the Microsoft hosting and logging extensions and use the Microsoft.Extensions.Logging.Log4Net.AspNetCore package.

Introduction

Illustrates customizing Log4Net by passing in a custom Appender.

Configure Log4Net

var layout = new PatternLayout
{
    ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
};
layout.ActivateOptions();
var consoleAppender = new ConsoleAppender
{
    Threshold = Level.Info,
    Layout = layout
};
consoleAppender.ActivateOptions();
var executingAssembly = Assembly.GetExecutingAssembly();
var repository = log4net.LogManager.GetRepository(executingAssembly);
BasicConfigurator.Configure(repository, consoleAppender);

Pass that configuration to NServiceBus

LogManager.Use<Log4NetFactory>();

// Then continue with the endpoint configuration
var endpointConfiguration = new EndpointConfiguration("Samples.Logging.Log4NetCustom");

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