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).
LogManager contains methods and properties to create loggers and manage logging configuration. Click on each method to see the API documentation:
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.
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.
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.
The following methods and properties let you determine whether logging is enabled for the specified level:
Full list of members of the Logger class can be found here.