.NET logging API
If you want to contribute, please create a user account and contact Jarek to get edit access.
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 is a concrete class not an interface.
Contents |
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 the logger instance for each class. The name 'logger' might not be a bad option.
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.
- 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.
Examples
Simple Logging
The most basic logging involves sending a string to one of the Logger methods.
using System; using System.Globalization; using NLog; class MyClass { static Logger logger = LogManager.GetLogger("MyClass"); public void LogSomething() { logger.Fatal("Something really bad happened"); } }
Custom Logging
If you want more control over the values passed to your logging target, you can manually instantiate a LogEventInfo object and modify it's properties, and then log it with the Logger.
using System; using System.Globalization; using NLog; class MyClass { static Logger logger = LogManager.GetLogger("MyClass"); public void LogSomething() { LogEventInfo myEvent = new LogEventInfo(LogLevel.Debug, "", "My debug message"); myEvent.LoggerName = logger.Name; myEvent.Properties.Add("MyCustomValue", "This is from MyClass"); logger.Log(myEvent); } }
Full list of members of the Logger class can be found here.




