.NET API
NLog uses a simple logging API which is similar to the one provided by
log4net.
There are 2 classes that you interact with: LogManager and Logger.
Note that Logger a concrete class not an interface (like ILog from log4net).
Log Manager
LogManager contains methods and properties to create loggers and manage logging configuration. Click on each method to see the API documentation:
- LogManager.GetLogger - gets or creates the specified logger
- LogManager.GetCurrentClassLogger - gets or creates the logger for currently executing class
- LogManager.Configuration - gets or sets the current logging configuration information
- LogManager.GlobalThreshold - gets or sets the global logging threshold
Full list of members of the LogManager class can be found here.
It's recommended to use LogManager.GetLogger("loggerName")
to create a logger for each class, store it in a static field and use for logging. We recommend having a consistently-named
variable that holds logger instance for each class. logger might not be a bad option for the logger
name.
The following example shows the code that uses the recommended way of creating and keeping logger instance.
using System;
using System.Globalization;
using NLog;
class MyClass {
static Logger logger = LogManager.GetLogger("MyClass");
// other class members go here
}
It's also possible to use or
LogManager.GetCurrentClassLogger()
but this feature isn't supported in Compact Framework configuration, so if you want
to support mobile devices you shouldn't use this syntax. GetCurrentClassLogger is also quite
costly because internally it uses the StackTrace class to get the name of the
current class.
using System;
using System.Globalization;
using NLog;
class MyClass {
static Logger logger = LogManager.GetCurrentClassLogger();
// class members go here
}Logger
The NLog.Logger class has the following methods. Each method has a number of overloads designed to minimize the number of memory allocations in order to improve logging speed. Click on a method name to see the list of overloads for it.
- Log() - Writes the diagnostic message at the specified level using the specified format provider and format parameters.
- Trace() - Writes the diagnostic message at the Trace level using the specified format provider and format parameters.
- Debug() - Writes the diagnostic message at the Debug level using the specified format provider and format parameters.
- Info() - Writes the diagnostic message at the Info level using the specified format provider and format parameters.
- Warn() - Writes the diagnostic message at the Warn level using the specified format provider and format parameters.
- Error() - Writes the diagnostic message at the Error level using the specified format provider and format parameters.
- Fatal() - Writes the diagnostic message at the Fatal level using the specified format provider and format parameters.
The following methods and properties let you determine whether logging is enabled for the specified level:
- IsEnabled() - Determines if logging is enabled for the specified level.
- IsTraceEnabled - Determines if logging is enabled for the Trace level.
- IsDebugEnabled - Determines if logging is enabled for the Debug level.
- IsInfoEnabled - Determines if logging is enabled for the Info level.
- IsWarnEnabled - Determines if logging is enabled for the Warn level.
- IsErrorEnabled - Determines if logging is enabled for the Error level.
- IsFatalEnabled - Determines if logging is enabled for the Fatal level.
Full list of members of the Logger class can be found here.


