Logging from the user code

Component: NServiceBus
NuGet Package NServiceBus (7.2 - 7.3)

The NServiceBus logging abstractions can be used for writing log messages from the user code. This approach ensures that both NServiceBus and user code log messages are written to the same destinations.

Set up a single static field to an ILog in the classes, and then use it in all methods:

public class ClassUsingLogging
    static ILog log = LogManager.GetLogger<ClassUsingLogging>();

    public void SomeMethod()
        log.Warn("Something unexpected happened.");
        if (log.IsDebugEnabled)
            log.Debug("Something expected happened.");
Make sure that logging is correctly initialized before resolving the ILog instance. Not doing so can result in a logger using an incorrect configuration
To avoid unnecessary processing, especially when logging more verbose messages, such as Debug, make sure to first check if logging at that level is enabled.
Since LogManager.GetLogger(..); is an expensive call, it is important that the field is static so that the call happens only once per class and has the best possible performance.

Last modified