NServiceBus has some limited, and opinionated, logging built in.
The default logging behavior is as follows:
This is applicable to both self hosting and using the NServiceBus Host
Info (and above) messages will be piped to the current console.
Errors will be written with
ConsoleColor.Red. Warnings will be written with
ConsoleColor.DarkYellow. All other message will be written with
Warn (and above) messages will be written to
Info (and above) messages will be written to a rolling log file.
This file will keep 10MB per file and a maximum of 10 log files.
The default logging directory will be
HttpContext.Current.Server.MapPath("~/App_Data/") for websites and
AppDomain.CurrentDomain.BaseDirectory for all other processes.
The default file name will be
N is a sequence number for when the log file reaches the max size.
With code both the Level and the logging directory can be configured. To do this, use the
var defaultFactory = LogManager.Use<DefaultFactory>(); defaultFactory.Directory("pathToLoggingDirectory"); defaultFactory.Level(LogLevel.Debug);
The Logging level, or "Threshold", indicates the log levels that will be outputted. So for example a value of
Warn would mean all
Fatal message would be outputted.
The supported logging levels are
app.configand code, the code configuration will be applied.
IProvideConfigurationis not recommended in Version 6, use the code configuration API instead. The configuration sections will be removed in Version 7.
var defaultFactory = LogManager.Use<DefaultFactory>(); defaultFactory.Level(LogLevel.Debug);
For more advanced logging, it is recommended to utilize one of the many mature logging libraries available for .NET.
- Log4Net integration
- NLog integration
- CommonLogging integration
- Serilog integration
- EventSourceLogging integration
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
Mainof a console app or windows service.
- At the start of the constructor of the class that implements
IConfigureThisEndpointwhen using NServiceBus.Host.
- At the start of the
Global.Application_Startin a asp.net application.