Last updated: 2006-09-18
This website is based on NLog v20060918.
Click here to view the documentation
for other versions.
| NLog Configuration OptionsNLog can be configured in many ways, both programmatically and using config files. This document describes the available configuration options. Logging configurationNLog attempts to automatically configure itself on startup, by looking for the configuration files in some standard places. The following locations will be searched when executing a stand-alone *.exe application:
In case of an ASP.NET application, the following files are searched:
The .NET Compact Framework doesn't recognize application configuration files (*.exe.config) nor environmental variables, so NLog only looks in these locations:
Configuration file formatNLog supports two configuration file formats:
In the first variant, we use a standard configSections mechanism, which makes our file look like this: The simplified format is the pure XML having the <nlog /> element as its root. The use of namespaces is optional, but it enables the Intellisense in Visual Studio. Note that NLog config file is case-insensitive when not using namespaces and is case-sensitive when you use them. Intellisense only works with case-sensitive configurations. Configuration elementsYou can use the following elements as children to <nlog />. The first two elements from the list are required to be present in all NLog configuration files, the remaining ones are optional and can be useful in advanced scenarios.
TargetsThe <targets /> section defines log targets/outputs. Each target is represented by a <target /> element. There are two attributes required for each target:
In addition to these attributes, targets usually accept other parameters, which influence the way diagnostic traces are written. Each target has a different set of parameters, they are described in detail on project's homepage and they are context-sensitive. Intellisense is also available in Visual Studio. For example - the "File" target accepts the fileName parameter which defines output file name and the Console target has the error parameter which determines whether the diagnostic traces are written to standard error (stderr) instead of standard output (stdout) of the process. This example demonstrates a <targets /> section which defines multiple targets: two files, one network target and OutputDebugString target: NLog provides many predefined targets. They are described on the project's homepage. It's actually very easy to create your own target - it requires about 15-20 lines of code and is described in the documentation. RulesLog routing rules are defined in the <rules /> section. It is a simple routing table, where we define the list of targets that should be written to for each combination of source/logger name and log level. Rules are processed starting with the first rule in the list. When a rule matches, log messages are directed to target(s) in that rule. If a rule is marked as final, rules beneath the current rule are not processed. Each routing table entry is a <logger /> element, which accepts the following attributes:
Some examples:
In the simplest cases the entire logging configuration consists of a single <target /> and a single <logger /> rule that routes messages to this target depending on their level. As the application grows, adding more targets and rules is very simple. Contextual informationOne of NLog's strongest assets is the ability to use layouts. They include pieces of text surrounded by a pair of "${" (dollar sign + left curly brace) and "}" (right curly brace). The markup denotes "layout renderers" which can be used to insert pieces of contextual information into the text. Layouts can be used in many places, for example they can control the format of information written on the screen or sent to a file, but also to control the file names themselves. This is very powerful, which we'll see in a moment. Let's assume, that we want to augment each message written to the console with:
This is very easy: We can make each messages for each logger go to a separate file, as in the following example: Ay you can see the ${logger} layout renderer was used in the fileName attribute, which causes each log message to be written to the file whose name includes the logger name. The above example will create the following files:
It's a frequent requirement to be able to keep log files for each day separate. This is trivial, too, thanks to the ${shortdate} layout renderer: How about givin each employee their own log file? The ${windows-identity} layout renderer will do the trick: Thanks to this simple setting NLog will create a set of files named after our employees' logins:
More complex cases are of course possible. The following sample demonstrates the way of creating a distinct log file for each person per day. Log files for each day are stored in a separate directory: This creates the following files:
NLog provides many predefined layout renderers. They are described on the http://www.nlog-project.org/layoutrenderers.html page. It's very easy to create your own layout renderer. It just takes 15-20 lines of code and is described in the documentation section of the project website. Include filesIt's sometimes desired to split the configuration file into many smaller ones. NLog provides an include file mechanism for that. To include an external file, you simply use It's worth noting that the fileName attribute, just like most attributes in NLog config file(s), may include dynamic values using the familiar ${var} notation, so it's possible to include different files based on environmental properties. The following configuration example demonstrates this, by loading file whose name is derived from the name of the machine we're running on. Variables let us write complex or repeatable expression (such as file names) in a concise manner. To define a variable we use the <variable name="var" value="xxx" /> syntax. Once defined, variables can be used as if they were layout renderers - by using ${var} syntax, as demonstrated in the following example: Automatic reconfigurationThe configuration file is read automatically at program startup. In a long running process (such as a Windows service or an ASP.NET application) it's sometimes desirable to temporarily increase the log level without stopping the application. NLog can monitor logging configuration files and re-read them each time they are modified. To enable this mechanism, you simply set <nlog autoReload="true" /> in your configuration file. Note that automatic reconfiguration supports include files, so each time one of the files include is changed, the entire configuration gets reloaded. Troubleshooting loggingSometimes our application doesn't write anything to the log files, even though we have supposedly configured logging properly. There can be many reasons for logs not being written. The most common problem are permissions, usually in an ASP.NET process, where the aspnet_wp.exe or w3wp.exe process may not have write access to the directory where we want to store logs. NLog is designed to swallow run-time exceptions that may result from logging. The following settings can change this behavior and/or redirect these messages.
Asynchronous processing, wrapper and compound targetsNLog provides wrapper and compound targets which modify other targets' behaviour by adding features like:
To define a wrapper or compound target in the configuration file, simply nest a target node within another target node. You can even wrap a wrapper target. There are no limits on depth. For example, to add asynchronous logging with retry-on-error functionality add this to your configuration file: Because asynchronous processing is a common scenario, NLog supports a shorthand notation to enable it for all targets without the need to specify explicit wrappers. You simply set <targets async="true" /> and all your targets will be wrapped with the AsyncWrapper target. Default wrappersSometimes we require ALL targets to be wrapped in the same way, for example to add buffering and/or retrying. NLog provides <default-wrapper /> syntax for that. You simply put this element in the <targets /> section and all your targets will be automatically wrapped with the specified wrapper. Note that <default-wrapper /> applies to the single <targets /> section only and you can have multiple sections so you can define groups of targets that are wrapped in a similar manner. In the above example we've defined two buffered File targets and three asynchronous and retrying Network targets. Default target parametersSimilar to default wrappers NLog provides <default-target-parameters /> which enables you to specify default values of target parameters. For example, if you don't want files to be kept open, you can either add keepFileOpen="false" to each target, as in the following example: Alternatively you can specify a single <default-target-parameters /> that applies to all targets in the <targets /> section. Default parameters are defined on a per-type basis and are applied BEFORE the actual attributes defined in the XML file: Last updated: 2006-07-10 11:32:55 | |||||||
| Copyright © 2004-2006 by Jarosław Kowalski. |