-
Notifications You must be signed in to change notification settings -
Fork 1.4k
Configuration file
-
standard application configuration file app.config (Ex. applicationname.exe.config) -
applicationname.exe.nlog in application’s directory -
NLog.config in application’s directory -
NLog.dll.nlog in a directory where NLog.dll is located (only if NLog isn't installed in the GAC)
-
standard web application configuration file web.config -
web.nlog located in the same directory as web.config -
NLog.config in application’s directory -
NLog.dll.nlog in a directory where NLog.dll is located (only if NLog isn't installed in the GAC)
<? xml version = " one " encoding = " utf-8 " ?> < nlog xmlns = " http://www.nlog-project.org/schemas/NLog.xsd "
xmlns : xsi = " http://www.w3.org/2001/XMLSchema-instance " > < targets > < target name = " logfile " xsi : type = " File " fileName = " file.txt " /> < target name = " logconsole " xsi : type = " Console " /> </ targets > < rules > < logger name = " * " minlevel = " Info " writeTo = " logconsole " /> < logger name = " * " minlevel = " Debug " writeTo = " logfile " /> </ rules > </ nlog >
< configuration > < configSections > < section name = " nlog " type = " NLog.Config.ConfigSectionHandler, NLog " /> </ configSections > ... < nlog > < targets > < target name = " logconsole " xsi : type = " Console " /> </ targets > < rules > < logger name = " * " minlevel = " Info " writeTo = " logconsole " /> </ rules > </ nlog > </ configuration >
-
targets – defines log targets/outputs -
rules – defines log routing rules -
extensions – loads NLog extensions from the *.dll file -
include – includes external configuration file -
variable – sets the value of a configuration variable
-
name – target name -
type – target type – such as "File", "Database", "Mail". When using namespaces this attribute is named xsi:type .
< targets > < target name = " f1 " xsi : type = " File " fileName = " file1.txt " /> < target name = " f2 " xsi : type = " File " fileName = " file2.txt " /> < target name = " n1 " xsi : type = " Network " address = " tcp://localhost:4001 " /> < target name = " ds " xsi : type = " OutputDebugString " /> </ targets >
|
|
|
|
---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
name – Match logger-name of the Logger-object - may include wildcard characters (* and ?) -
minlevel – minimum level to log (matches the specified level and levels above) -
maxlevel – maximum level to log (matches the specified level and levels below) -
level – single level to log -
levels - comma separated list of levels to log -
writeTo – comma separated list of targets to write to -
final – no rules are processed after a final rule matches -
enabled - set to false to disable the rule without deleting it -
ruleName - rule identifier to allow rule lookup with Configuration.FindRuleByName and Configuration.RemoveRuleByName . Introduced in NLog 4.6.4 -
finalMinLevel - Loggers matching will be restricted to specified minimum level for following rules. Introduced in NLog 5.0
< targets > < target name = " logconsole " xsi : type = " Console " /> </ targets > < rules > < logger name = " * " minlevel = " Info " writeTo = " logconsole " /> </ rules >
-
* - matches 0 or more characters -
? - matches exactly 1 character
< logger name = " Name.Space.* " writeTo = " target1 " />
< logger name = " Name.Space.Class1 " writeTo = " target2 " /> <!-- Add final=true to stop output to target1 --> < logger name = " Name.Space.* " writeTo = " target1 " />
< logger name = " Name.Space.Class2 " maxlevel = " Warn " final = " true " /> <!-- Blackhole that stops all but Error + Fatal --> < logger name = " Name.Space.* " writeTo = " target1 " />
< logger name = " Name.Space.Class3 " maxlevel = " Off " final = " true " /> <!-- Blackhole that stops everything --> < logger name = " Name.Space.* " writeTo = " target1 " />
< rules > < logger name = " * " minlevel = " Info " writeTo = " logconsole " /> < logger name = " Name.Space.* " minlevel = " Debug " writeTo = " f1 " /> < logger name = " *. Class1 " minlevel = " Trace " writeTo = " f2 " /> < logger name = " *. Library.* " minlevel = " Warn " writeTo = " f3 " /> </ rules >
< rules > < logger name = " *TcpTestServer[*]. Connection[07].* " minlevel = " Trace " writeTo = " logconsole " final = " true " /> < logger name = " *TcpTestServer[*]. Connection[??].* " minlevel = " Debug " writeTo = " logconsole " final = " true " /> < logger name = " * " minlevel = " Info " writeTo = " logconsole " /> </ rules >
level levels -
minlevel and maxlevel (these have the same priority) -
none (all levels are logged)
-
NLog 4.6 added support for using NLog Config Variables to configure level attributes. ( level , minlevel etc):
< variable name = ' globalLevel ' value = ' debug ' /> < rules > < logger minlevel = ' ${globalLevel} ' /> </ rules >
-
NLog 4.6.4 added support for lookup of Logging Rules using RuleName using Configuration API -
NLog 4.6.7 added support for using NLog Layout like ${gdc:globalLevel} to dynamically change level attributes at runtime. See also Semi Dynamic Routing Rules
< variable name = " varname " value = " xxx " />
< nlog > < variable name = " logDirectory " value = " logs/${shortdate} " /> < targets > < target name = " file1 " xsi : type = " File " fileName = " ${logDirectory}/file1.txt " /> < target name = " file2 " xsi : type = " File " fileName = " ${logDirectory}/file2.txt " /> </ targets > </ nlog >
-
Constant mode - ${varName} -
Dynamic mode - ${var:varName} (Introduced with NLog 4.1)
-
Variables can be changed, deleted and created from the Configuration API -
A default value can be configured for a variable, e.g. ${var:password:default=unknown} -
By default all variables assigned at runtime are kept even after configuration reloads. Unless having configured keepVariablesOnReload="false" . NLog 4.4 introduced keepVariablesOnReload and before NLog 5.0 the default value was false
< nlog > < variable name = " myJsonLayout " > < layout type = " JsonLayout " > < attribute name = " message " value = " ${messsage} " > </ layout > </ variable > < targets > < target name = " debug " type = " Debug " layout = " ${myJsonLayout} " /> </ targets > < rules > < logger name = " * " minlevel = " Debug " writeTo = " debug " /> </ rules > </ nlog >
-
current date and time -
name of the class and method that emitted the log message -
log level -
message text
< target name = " c " xsi : type = " Console " layout = " ${longdate} ${callsite} ${level} ${message} " />
< target name = " f " xsi : type = " File " fileName = " ${logger}.txt " />
-
Name.Space.Class1.txt -
Name.Space.Class2.txt -
Name.Space.Class3.txt -
Other.Name.Space.Class1.txt -
Other.Name.Space.Class2.txt -
Other.Name.Space.Class3.txt
-
asynchronous processing (wrapped target runs in a separate thread) -
retry-on-error -
load balancing -
buffering -
filtering -
failover (failover)
< targets > < target name = " n " xsi : type = " AsyncWrapper " > < target xsi : type = " RetryingWrapper " > < target xsi : type = " File " fileName = " ${file}.txt " /> </ target > </ target > </ targets >
< nlog > < targets async = " true " > <!-- all targets in this section will automatically be asynchronous --> </ targets > </ nlog >
< nlog > < targets > < default-wrapper xsi : type = " BufferingWrapper " bufferSize = " one hundred " /> < target name = " f1 " xsi : type = " File " fileName = " f1.txt " /> < target name = " f2 " xsi : type = " File " fileName = " f2.txt " /> </ targets > < targets > < default-wrapper xsi : type = " AsyncWrapper " > < wrapper-target xsi : type = " RetryingWrapper " /> </ default-wrapper > < target name = " n1 " xsi : type = " Network " address = " tcp://localhost:4001 " /> < target name = " n2 " xsi : type = " Network " address = " tcp://localhost:4002 " /> < target name = " n3 " xsi : type = " Network " address = " tcp://localhost:4003 " /> </ targets > </ nlog >
< nlog > < targets > < target name = " f1 " xsi : type = " File " fileName = " f1.txt " keepFileOpen = " false " /> < target name = " f2 " xsi : type = " File " fileName = " f2.txt " keepFileOpen = " false " /> < target name = " f3 " xsi : type = " File " fileName = " f3.txt " keepFileOpen = " false " /> </ targets > </ nlog >
< nlog > < targets > < default-target-parameters xsi : type = " File " keepFileOpen = " false " /> < target name = " f1 " xsi : type = " File " fileName = " f1.txt " /> < target name = " f2 " xsi : type = " File " fileName = " f2.txt " /> < target name = " f3 " xsi : type = " File " fileName = " f3.txt " /> </ targets > </ nlog >
< nlog > < extensions > < add assembly = " MyAssembly " /> </ extensions > < targets > < target name = " a1 " type = " MyTarget " host = " localhost " /> </ targets > < rules > < logger name = " * " minlevel = " Info " appendTo = " a1 " /> </ rules > </ nlog >
< nlog > < targets > < target name = " a1 " type = " MyTarget, MyAssembly " host = " localhost " /> </ targets > < rules > < logger name = " * " minlevel = " Info " appendTo = " a1 " /> </ rules > </ nlog >
< nlog autoReload = " true " > ... </ nlog >
< include file = " nlog-common.config " />
< include file = " ${machinename}.config " ignoreErrors = " true " />
-
: is the value separator. -
} is the end of the layout
-
${appdomain:format={1\}{0\}} (escape of } ) -
${rot13:inner=${scopenested:topFrames=3:separator=x}} (no escaping needed) -
${when:when=1 == 1:Inner=Test\: Hello} (escape of : )
-
<nlog throwExceptions="true" /> - adding the throwExceptions attribute in the config file causes NLog to stop masking exceptions and pass them to the calling application instead. This attribute is useful at deployment time to quickly locate any problems. It is critical that one configure throwExceptions to "false" as soon as the application is properly configured to run, so accidental logging problems will not cause application to hang or crash. -
<nlog throwConfigExceptions="true" /> - Similar to throwExceptions but isolated to configuration exceptions only. Unlike throwExceptions then it is not dangerous to have enabled, and is recommended for production environments where it is critical that initial setup of logging configuration succeeds. Introduced in NLog 4.3. Default null (Means not configured and instead use value of throwExceptions ) -
<nlog internalLogLevel="Trace|Debug|Info|Warn|Error|Fatal" /> – determines the internal log level. The higher the level, the less verbose the internal log output. The output is usually very helpful when needing to diagnose logging issues. See Internal-logging for options to setup output to File or Console.
-
Getting started -
How to use structured logging -
Troubleshooting -
FAQ
-
All targets, layouts and layout renderers
Popular: -
Using NLog with NLog.config -
Using NLog with appsettings.json