NLog is a log routing engine. It processes log messages that are output by the components of your program, formats them according to your preference, filters them according to their level, source and content and outputs them to one or more place based on a set of rules.
To write a log message in your code you use an instance of a Logger which you acquire from
a LogManager. You can think of loggers as of log sources and targets as log sinks. Typically
there's one source for each class named after its full name including namespace, e.g.
MyCompany.MyProject.NameSpace.ClassName but you may invent your own naming schema as well.
Each message that you output has a level which describes its importance.
There are 6 predefined logging levels. They are: Trace, Debug, Info, Warn, Error, Fatal. Unlike other logging frameworks
NLog does NOT support introducing more logging levels, but I believe that in this case "less is more".
The following table describes the allowed logging levels along with their recommended usage.
| Log Level | Meaning |
|---|---|
| Trace | Messages useful to programmer only, not normally enabled, even by the support team. This level can include high-volume messages like the dump of entire datasets, XML documents, detailed parameter information, etc. |
| Debug | Messages useful for debugging, not normally enabled. This level can include high-traffic messages like the ones you write at each method call. |
| Info | Normal informational messages. Typically low traffic information - like "user logged on", "user authenticated". |
| Warn | Warning messages. |
| Error | Error messages. |
| Fatal | Fatal error messages. You usually write just one such message - just before the application terminates. |
A routing table is used to manage the log rules. It can be accessed either programmatically
or as an XML document either standalone or embedded in the application configuration file App.config.
The routing table is made up of the following main parts:
Each
As you can see the routing table is quite clean and simple, but very powerful. Some examples of what you can use with it include:
It's also possible to use filters and final rules, and this is described below in advanced section.
Layouts are one of the coolest features of NLog. They let you annotate log messages with environmental information like:
Each target has a parameter called layout that describes the log output formatting. By default
it is:
${longdate}|${level:uppercase=true}|${logger}|${message}
so that each log line contains the data, log level, logger name and log message. This can be extended, for example, to provide some particular value from ASP.NET session state, thread id and call site (class and name of the invoking method):
${longdate} ${level:uppercase=true} ${logger} ${aspnet-session:variable=UserName} ${threadid} ${callsite} ${message}
Now, each logging message (a line in the output file) will contain all of the above useful information. Example of a real-life configuration that does just that and writes output to both a file and ASP.NET trace facility. Note how the file output is more detailed than ASP.NET trace output:
It's even possible to write each log message to a different file based on environmental properties.
For example, to write to a new file each day separated by the log level, just change filename="" to:
filename="${basedir}/../logs/${date:format=yyyy-MM-dd}_${level}_log.txt"
The resulting files will be named:
../logs/2005-05-29_Debug_log.txt../logs/2005-05-29_Info_log.txt../logs/2005-05-29_Warn_log.txt../logs/2005-05-29_Error_log.txt../logs/2005-05-29_Fatal_log.txt../logs/2005-05-30_Debug_log.txt../logs/2005-05-30_Info_log.txt../logs/2005-05-30_Warn_log.txt../logs/2005-05-30_Error_log.txt../logs/2005-05-30_Fatal_log.txtYou can probably see that the possibilities are endless. See the Layout Renderers for more information.
Usage is simple. Just get an instance of a Logger from the LogManager and
write your messages to it like you do with Console.WriteLine(). To write a message at a particular
level use a method that's named after the level.
The following code example shows what's available:
There are cases where you need to debug your logging configuration. This is usually the case where you don't get the expected log output. NLog provides a feature called internal logging. Internal logging lets you write NLog-internal debugging messages to the console and/or a log file optionally specifying the log level verbosity. Additionally you may want to tell NLog to throw exceptions on any error. This can help you detect and fix configuration errors (mostly permission problems) easily.
The following options can be used in the configuration file to enable internal debugging:
More information about internal logging can be found in the troubleshooting section and in the configuration section.
If you have many applications you may want to share their logging configurations. NLog provides
There are cases where you need to modify the logging configuration at application runtime. Having to restart your application in this case should be avoided especially on production servers. NLog provides a convenient configuration attribute for this. Just put the following in the configuration file:
See the Filter Reference for more information. Tutorial will be added here soon.
By default all NLog rules are additive, that is their effects add up. The rules are processed top-down
from the first rule to the last one. Sometimes you want to quit rule processing. That's what the final attribute
is used for. [TODO - more to be written].