Logging in NServiceBus

Default Logging

NServiceBus has some limited, and opinionated, logging built in.

The default logging behavior is as follows:

Version 3 and 4 Defaults

In these versions default logging was only enabled in the NServiceBus Host. For self hosting you must enable the logging yourself using code.

See NServiceBus Host Profiles for the default logging in the NServiceBus Host.

Version 5 and Higher Defaults

This is applicable to both self hosting and using the NServiceBus Host

Console

All Info (and above) messages will be piped to the current console.

Errors will be written with ConsoleColor.Red. Warnings will be written with ConsoleColor.DarkYellow. All other message will be written with ConsoleColor.White.

Trace

All Warn (and above) messages will be written to Trace.WriteLine.

Rolling File

All Info (and above) messages will be written to a rolling log file.

This file will keep 10MB per file and a maximum of 10 log files.

The default logging directory will be HttpContext.Current.Server.MapPath("~/App_Data/") for websites and AppDomain.CurrentDomain.BaseDirectory for all other processes.

The default file name will be nsb_log_yyyy-MM-dd_N.txt, where N is a sequence number for when the log file reaches the max size.

Logging Levels

The Logging level, or "Threshold", indicates the log levels that will be outputted. So for example a value of Warn would mean all Warn, Error and Fatal message would be outputted.

In NServiceBus Version 5 and higher the supported logging levels are

  • Debug
  • Info
  • Warn
  • Error
  • Fatal

In NServiceBus Versions 3 and 4 the supported Levels are defined by the Log4Net.Level class.

Changing the Logging Level

When logging level is defined in both app.config and code:

  • In Version 4 and earlier the app.config wins.
  • In Version 5 and higher the code wins.

Via app.config

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

Via IProvideConfiguration

public class ProvideConfiguration : IProvideConfiguration<Logging>
{
    public Logging GetConfiguration()
    {
        return new Logging
        {
            Threshold = "Info"
        };
    }
}
public class ProvideConfiguration : IProvideConfiguration<Logging>
{
    public Logging GetConfiguration()
    {
        return new Logging
        {
            Threshold = "Info"
        };
    }
}

Via config API

DefaultFactory defaultFactory = LogManager.Use<DefaultFactory>();
defaultFactory.Level(LogLevel.Debug);
DefaultFactory defaultFactory = LogManager.Use<DefaultFactory>();
defaultFactory.Level(LogLevel.Debug);

The above API is only available in Version 5 and higher. In earlier versions you can achieve the same by taking full control over the Log4Net integration.

Changing the defaults

Changing settings via code

With code you can configure both the Level and the logging directory. To do this, use the LogManager class.

DefaultFactory defaultFactory = LogManager.Use<DefaultFactory>();
defaultFactory.Directory("pathToLoggingDirectory");
defaultFactory.Level(LogLevel.Debug);
DefaultFactory defaultFactory = LogManager.Use<DefaultFactory>();
defaultFactory.Directory("pathToLoggingDirectory");
defaultFactory.Level(LogLevel.Debug);

Ensure you do this before any bus configuration is done.

In earlier versions you can achieve the same by taking full control over the Log4Net integration.

Custom Logging

For more advanced logging, it is recommended that you utilize one of the many mature logging libraries available for .Net.

Moving to custom logging means none of the approaches used in the above Default Logging apply.

When to configure logging

It is important to configure logging before any bus configuration is done since logging is configured in the static context of each NServiceBus class. So it should be configured at the startup of your app. For example

  • At the start of the Main of a console app or windows service.
  • At the start of the constructor of the class that implements IConfigureThisEndpoint when using NServiceBus.Host.
  • At the start of your Global.Application_Start in a asp.net application.

Samples

Related Articles


Last modified 2015-11-10 11:50:35Z