Getting Started
Architecture
NServiceBus
Transports
Persistence
Hosting
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

Log4Net

NuGet Package: NServiceBus.Log4Net 3.x
Target Version: NServiceBus 7.x
Standard support for version 7.x of NServiceBus has expired. For more information see our Support Policy.

Logging integration into the host

When using NServiceBus 10.2 or later with the .NET Generic Host, configure Log4Net directly on the host builder. The bridge package is not required:

var builder = Host.CreateApplicationBuilder();

builder.Logging.AddLog4Net();

builder.Services.AddNServiceBusEndpoint(endpointConfiguration);

For more information, see Hosting with Microsoft.Extensions.Hosting.

Usage

var layout = new PatternLayout
{
    ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
};
layout.ActivateOptions();
var consoleAppender = new ConsoleAppender
{
    Threshold = Level.Debug,
    Layout = layout
};
consoleAppender.ActivateOptions();

var executingAssembly = Assembly.GetExecutingAssembly();
var repository = log4net.LogManager.GetRepository(executingAssembly);
BasicConfigurator.Configure(repository, consoleAppender);

LogManager.Use<Log4NetFactory>();

Filtering

NServiceBus can write a significant amount of information to the log. To limit this information, use the filtering features of the underlying logging framework.

For example, to limit log output to a specific namespace.

The following code example adds a filter.

The Filter

public class NServiceBusLogFilter :
    FilterSkeleton
{
    public override FilterDecision Decide(LoggingEvent loggingEvent)
    {
        if (loggingEvent.LoggerName.StartsWith("NServiceBus."))
        {
            if (loggingEvent.Level < Level.Warn)
            {
                return FilterDecision.Deny;
            }
        }
        return FilterDecision.Neutral;
    }
}

Using the Filter

var appender = new ConsoleAppender
{
    Threshold = Level.Debug,
    Layout = new SimpleLayout(),
};

appender.AddFilter(new NServiceBusLogFilter());
appender.ActivateOptions();

var executingAssembly = Assembly.GetExecutingAssembly();
var repository = log4net.LogManager.GetRepository(executingAssembly);
BasicConfigurator.Configure(repository, appender);

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

Additional exception data

Starting with NServiceBus version 7.2, exceptions from failing message handlers might contain additional error information in the Exception.Data property. Log4Net does not log this information by default, but it can be configured to do so using a custom PatternLayoutConverter:

class ExceptionDataConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var exceptionData = loggingEvent.ExceptionObject?.Data;
        if (exceptionData != null)
        {
            foreach (var key in exceptionData.Keys)
            {
                writer.WriteLine("{0}: {1}", key, exceptionData[key]);
            }
        }
    }
}

The custom converter can then be registered and incorporated into the log layout:

var layout = new PatternLayout
{
    ConversionPattern = "%d %-5p %c - %m%n%exception_data"
};
layout.AddConverter("exception_data", typeof(ExceptionDataConverter));
layout.ActivateOptions();