Writing a log entry

Component: NServiceBus
NuGet Package NServiceBus (7.x - 7.1)

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.Debug("Something interesting 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
When writing to a logger, ensure the log level is set to a value that will result in that log entry being written. For example, when calling .Debug(..) ensure that the log level is set to DEBUG. See Change settings via configuration.
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