NServiceBus.Log4Net is obsolete. NServiceBus is now providing support for logging libraries through the Microsoft.Extensions.Logging. Please see Logging in .NET Core and ASP.NET Core for further details.
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.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 from NServiceBus version 7.2, exceptions from failing message handlers might contain additional error information in the Exception.
property. Log4Net does not log this information by default, but 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();