Log4Net

Project Hosting
NuGet Package NServiceBus.Log4Net (3-pre)
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Support for writing all NServiceBus log entries to Log4Net.

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.

Here is a code configuration example for adding 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.Accept;
    }
}

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>();

Samples


Last modified