NServiceBus uses log4net for its logging purposes. If you’ve run one the samples which are included with the NServiceBus package, you could be overwhelmed of all the logging that the NServiceBus.Host.exe prints to the console windows. If you’re just starting your journey with the NServiceBus, all the logging may make it hard to understand the samples, because there’s so much text. But, fortunately, it’s really easy to change the sample projects so that the Log4Net configuration is read from a configuration file. This way you can easily change how much log information is shown on the console windows.

The tutorial works with NServiceBus 2.0 RTM.

1. Modifying the source code


We’re going to change one of the NServiceBus samples so that a user can adjust the log4net logging through the configuration file.
Start by opening the FullDuplex-sample solution (located in nservicebus/samples/FullDuplex) –folder. You should see four projects:

  • MyClient
  • MyMessages
  • MyServer
  • MyServer.Tests

We’re going to change MyServer but this tutorial can be applied to the MyClient also. Open the MyServerEndpointConfig.cs and you should see this:

   1: public class ServerInit : IWantCustomInitialization
   2: {
   3:     public void Init()
   4:     {
   5:         Configure.Instance.RijndaelEncryptionService();
   6:     }
   7: }

We’re going to change two things. First, make the ServerInit-class to implement IWantCustomLogging. Then, add the following line in the Init-method:


So you should end up with a class like this:

   1: public class ServerInit : IWantCustomInitialization, IWantCustomLogging
   2: {
   3:     public void Init()
   4:     {
   5:         Configure.Instance.RijndaelEncryptionService();
   6:         NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);
   7:     }
   8: }
If you now build the project and run it, you should see an error in the console output:

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net'…

Lets fix this next.

2. Modifying the configuration file

We’re going to add a standard log4net section to the configuration file. This way you don’t have to recompile the project any more if you want to change how much log is shown in the console window.

First, locate the MyServer.dll.config –file. Then, in the configSections , add a new section for the log4net:

   1: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

Then add the log4net configuration outside the configSections. Here’s a sample configuration which shows only INFO-messages (and higher) on the console:

   1: <log4net debug="false">
   2:   <appender name="console" type="log4net.Appender.ConsoleAppender">
   3:     <layout type="log4net.Layout.PatternLayout">
   4:       <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n"/>
   5:     </layout>
   6:   </appender>
   7:   <root>
   8:     <level value="INFO"/>
   9:     <appender-ref ref="console"/>
  10:   </root>
  11: </log4net>
Save the configuration file and start the NServiceBus.Host.Exe. You should see much less log information.


It’s only a matter of two code changes to get the full control for your logging needs by making the NServiceBus.Host.exe to read the log4net configuration from the application’s configuration file.