Solutions    |    Downloads    |    License    |    Documentation    |    Training    |    Support    |    Customers    |    About Us

Generic Host Sample

Last Updated: Dec 18, 2012 07:03PM IST

This sample demonstrates the various kinds of logging that can be used with the NServiceBus Host process. Each project shows a different approach and there is no relationships between them.

It is important to understand that although NServiceBus uses log4net as its logging infrastructure, the default configuration model is different from log4net.

This default logging configuration model is shown in the project LoggingWithConfigurableTreshold.

LoggingWithConfigurableThreshold

Open the app.config file, notice the Logging configuration section at the top as well as its contents:

<section name="Logging" type="NServiceBus.Config.Logging, NServiceBus.Core" />

<Logging Threshold="WARN" />

This instructs NServiceBus to only output logs at a level of WARN or higher. Now look at the EndpointConfig.cs file which makes 2 calls to the log - one at a level of Debug, the other at the level of Warn. When you run the project (right click, Debug, Start new instance), you'll only see the entry saying "This should appear" as below:

Logging with configurable threshold

The rest of the logging configuration is already inside NServiceBus and can be controlled using profiles. We'll see later more about using profiles, but for some background on what profiles are and how to use them, you can find the information here.

Now let's see how we can go back to regular log4net configuration:

LoggingFromAppConfig

This project shows how to tell NServiceBus to use standard log4net configuration.

Open up the EndpointConfig.cs file in the LoggingFromAppConfig project:

public class EndpointConfig : IConfigureThisEndpoint, AsA_Client, IWantCustomLogging
{
    public void Init()
    {
        Console.WriteLine("I'm using the logging configured in the app.config.");
        NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);
    }
}

Notice that this class implements the NServiceBus interface IWantCustomLogging.

This instructs NServiceBus to stop using its standard logging configuration. In the Init method you can see the call to SetLoggingLibrary, which tells NServiceBus to use Log4Net but to use the XmlConfigurator from log4net. In short, this returns NServiceBus to the standard log4net model.

Open the app.config file and notice the regular log4net configuration in there:

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

<log4net debug="false">
  <appender name="console" type="log4net.Appender.ConsoleAppender">
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c [%x] &lt;%X{auth}&gt; - %m%n"/>
    </layout>
  </appender>
  <root>
    <level value="DEBUG"/>
    <appender-ref ref="console"/>
  </root>
</log4net>

This instructs log4net to output to the console; using a pattern including various information like the data and time, the number of the thread, and more; output calls at a level of DEBUG or higher.

The reason that this technique isn't used by default is that NServiceBus tries to prevent administrators from being able to accidentally change things they shouldn't touch - for example, the pattern. NServiceBus takes things that should be managed by developers and provides a code API for them.

This is shown in the next example:

Logging

While it is likely that we'll want the ability to change the logging of a process without having to recompile it - the most common scenario is switching between a few well-defined configurations.

These "well-defined configurations" are known as profiles in NServiceBus.

When you open up EndpointConfig.cs in the Logging project, this is what you'll see:

public class Endpoint : IConfigureThisEndpoint {}

public class MyProductionProfile : Production {}

public class MyProductionLogging : IConfigureLoggingForProfile<MyProductionProfile>
{
    public void Configure(IConfigureThisEndpoint specifier)
    {
        Console.WriteLine("I'm going to do my custom logging setup in here using my own profile.");
    }
}

Notice the signature of the MyProductionLogging class - it implements IConfigureLoggingForProfile<MyProductionProfile>. What this means is that when the profile MyProductionProfile is specified, this class will be invoked by NServiceBus to configure the logging for the process. In the Configure method, it is likely that you'll be calling NServiceBus.SetLoggingLibrary.Log4Net but passing in some other value.

For more information on how to using SetLoggingLibrary, see here.

Now open the properties of the Logging project, and click on the Debug tab. Here's what you should see:

Logging project properties

Notice the command line arguments - "Logging.MyProductionProfile".

This is how you specify which profile the host should use, via the command line. If you haven't read about profiles, that should be your next step.

For more information on logging, see here.

About NServiceBus    |    Contact Us    |    Privacy    |    Follow us on:   
Copyright 2010-2013 NServiceBus. All rights reserved
support@nservicebus.com
http://assets2.desk.com/raca8b478c9bd89640c451013350d59caa6b66ee1/javascripts/
nservicebus
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/en/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise