NServiceBus has a built-in logging mechanism that does not depend on any external libraries. While limited in terms of available log targets, this built-in mechanism is production-ready and offers defaults that are reasonable for most deployments. The built-in framework is available and used as default in all NServiceBus hosting modes (e.g. self-hosting or Windows Service). Regardless of if the built-in logging or a custom logging library is used under the hood, the NServiceBus logging abstractions can be used for logging in the user code. By default NServiceBus has three log targets configured:
Info (and above) messages are written to the current console if one is available in the hosting environment.
Errors will be written with
ConsoleColor.. Warnings will be written with
ConsoleColor.. All other message will be written with
Warn (and above) messages are written to
Trace. and therefore can be forwarded to any trace listener.
Info (and above) messages are written to file. NServiceBus maintains up to 10 log files, each up to 10 MB in size. When the current file becomes full, NServiceBus automatically switches to the next one. If all ten files are full, the oldest file is overwritten.
The default logging directory is
HttpContext. for websites and
AppDomain. for all other processes.
The default file name is
N is a sequence number for when the log file reaches the max size.
The built-in logging mechanism allows customizing the logging directory and applying a global filter/threshold for log entries.
Each log entry is associated with a level that describes how important and critical that entry is. The built-in levels are following (in order of increasing importance)
Configuring the global threshold to one of the levels described above means that all messages below that level are discarded. For example setting the threshold value to
Warn means that only
Fatal messages are written.
LogManager class is the entry point for the logging configuration. If needed, it allows using custom logging integrations (see below). It also allows customization of the default built-in logging. The
Use generic method returns the
LoggingFactoryDefinition-derived object that provides the customization APIs.
IProvideConfigurationis deprecated in Version 7.
var defaultFactory = LogManager.Use<DefaultFactory>(); defaultFactory.Level(LogLevel.Debug);
var defaultFactory = LogManager.Use<DefaultFactory>(); defaultFactory.Directory("pathToLoggingDirectory");
For custom logging it is recommended to use the Microsoft.Extensions.Logging package with a supported provider.
It is important to configure logging before any endpoint configuration is done since logging is configured in the static context of each NServiceBus class. It should be configured as early as possible at the startup of the app. For example
- At the start of the
Mainof a console app or windows service.
- At the start of the
Global.in a asp.net application.
- Using endpoint configuration API in an application hosted via NServiceBus Host
Unit testing of logging is supported by the