NServiceBus Host Logging Configuration

Component: NServiceBus Host
NuGet Package NServiceBus.Host (6.x)
Target NServiceBus Version: 5.x
Standard support for version 5.x of NServiceBus has expired. For more information see our Support Policy.
For endpoints using NServiceBus version 7 and above, it is no longer recommended to use the NServiceBus Host version 8. Use alternative approaches such as Generic Host, NServiceBus Windows Service or NServiceBus Docker Container instead. The host is still supported for existing endpoints to ease the transition phase to the new hosting models, refer to the upgrade guide for further details.

This article explains how to customize the logging configuration when using the NServiceBus host. For more details about logging configuration with the built-in profiles, refer to the NServiceBus.Host Profiles - Logging section.

Constructor of IConfigureThisEndpoint implementation

Logging should be customized in the constructor of the class that implements IConfigureThisEndpoint. This is recommended, as this class is the earliest opportunity to initialize any custom logging framework.

If logging is not initialized in the constructor and anything goes wrong during startup of the NServiceBus.Host, errors could be written to the default NServiceBus logging location, and not in the expected custom log output location(s).

Via endpoint configuration

To change the host's logging configuration, implement the IConfigureThisEndoint interface. Provide the custom configuration in the Customize method:

class CustomLogging : IConfigureThisEndpoint
{
    public void Customize(BusConfiguration configuration)
    {
        LogManager.Use<DefaultFactory>();
    }
}

Via profiles

Logging levels and sinks need to be defined before configuring other components, therefore logging profile configuration is kept separate from other profile behaviors and requires implementing a dedicated interface.

To customize logging for a given profile, create a class implementing IConfigureLoggingForProfile<T> where T is the profile type:

public class YourProfileLoggingHandler :
    NServiceBus.Hosting.Profiles.IConfigureLoggingForProfile<YourProfile>
{
    public void Configure(IConfigureThisEndpoint specifier)
    {
        // setup logging infrastructure
        LogManager.Use<Log4NetFactory>();
    }

}
One class can configure logging for multiple profile types. However, it is not possible to spread logging configuration for a single profile across multiple classes.

The host's profiles mechanism can be used to specify different logging levels (DEBUG, WARN, etc.) or targets (CONSOLE, FILE, etc.).

For more details refer to the Host Custom Logging, Host Profile Logging sample.

Samples

Related Articles


Last modified