Logging

Component: NServiceBus | Nuget: NServiceBus (Version: 3.x)
Standard support for version 3.x of NServiceBus has expired. For more information see our Support Policy.

Default Logging

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

The default logging behavior is as follows:

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

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

Changing the defaults

This can be achieved by taking full control over the Log4Net integration.

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.

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 the app.config wins.

Via app.config

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

Via IProvideConfiguration

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

Via config API

This can be achieved by taking full control over the Log4Net integration.

Custom Logging

For more advanced logging, it is recommended to 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 endpoint configuration is done since logging is configured in the static context of each NServiceBus class. So it should be configured at the startup of the 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 the Global.Application_Start in a asp.net application.

Samples

Related Articles


Last modified