Getting Colors in the log4net output from NServiceBus
I recently asked a question on the NServiceBus mailling list about how to get the ColoredConsoleAppender from log4net to work with the Generic Host in NServiceBus. By coincidence I stumbled upon a solution a few days ago, and thought I’d explain it here. The reason for getting it to work is of course to get some nicer output in the console when developing. Especially if you’re showing debug events it can be hard to sift through all of the information being presented; color helps with this.
Out of the box NServiceBus comes with three 3 profiles for the Generic Host: Lite, Integration and Production. These each set the logging level and the appender differently. I tried to customize this by utilizing the existing profile and just add log4net entries in my app.config file. This didn’t work well. I got a bunch of errors, which are also detailed in my question to the mailling list. However, if you instead implement your own profiles you can control the logging completely.
First we need to create our marker for our new profile:
using NServiceBus;
namespace Odin
{
public class Lite : IProfile {}
}
public class LiteProfileHandler : IHandleProfile<Lite>, IHandleProfile, IWantTheEndpointConfig
{
public void ProfileActivated()
{
Configure.Instance.Configurer.
ConfigureComponent<InMemorySagaPersister>(ComponentCallModelEnum.Singleton);
if (this.Config is AsA_Publisher)
{
Configure.Instance.Configurer.
ConfigureComponent<InMemorySubscriptionStorage>(ComponentCallModelEnum.Singleton);
}
}
public IConfigureThisEndpoint Config {get;set;}
}
public class LiteLoggingHandler : IConfigureLoggingForProfile<Lite>
{
public void Configure(IConfigureThisEndpoint specifier)
{
NServiceBus.SetLoggingLibrary.Log4Net(() => log4net.Config.XmlConfigurator.ConfigureAndWatch(
new FileInfo(@"C:\log4net.config")));
//Switch to this line of code to use the log4net section in app.config
//NServiceBus.SetLoggingLibrary.Log4Net(
// log4net.Config.XmlConfigurator.Configure
// );
}
}
NServiceBus.SetLoggingLibrary.Log4Net( log4net.Config.XmlConfigurator.Configure );
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net debug="false">
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] <%X{auth}> - %m%n"/>
</layout>
</appender>
<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="FATAL" />
<foreColor value="Red" />
<backColor value="White" />
</mapping>
<mapping>
<level value="ERROR" />
<foreColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="WARN" />
<foreColor value="Yellow" />
</mapping>
<mapping>
<level value="INFO" />
<foreColor value="Cyan" />
</mapping>
<mapping>
<level value="DEBUG" />
<foreColor value="Green" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="ColoredConsoleAppender"/>
</root>
</log4net>
</configuration>
To start the generic host with your new profile you parse the fully qualified type name to the NServiceBus.Host.Exe:
NServiceBus.Host.Exe Odin.Lite
And you should see an output console with color.
If you like my writing you should subscribe to my RSS feed.
I am a self employed independent software development consultant at 