NLog Is a simple and flexible .NET Logging class library ,NLog Of API Very similar to log4net
, And the configuration is very simple . through the use of NLog, We can be in any .NET Output debugging information with context in language , Configure the rules of deployment format and output target according to project requirements .
NLog Configure with routing , but log4net But using hierarchical appender to configure , So let's NLog The configuration file for is very easy to read , And easy to maintain in the future .
Support multiple forms of output log : text file system log data base Console mailbox etc. One Access method 1 Get this class : The first stay nuget Console input installation nlog command :
Install-Package NLog.Config
The second stay nuget On the package management interface , Search for installation Nlog and Nlog
configuration, This will automatically generate a config file , This and the program or web own config Use management in the future , No mutual influence
The third GitHub upper https://github.com/NLog/NLog/    
https://github.com/NLog/NLog/releases/ Two configuration file Support multiple configuration forms :
1 Directly using the application profile or web Profile for (app.config / web.config)
2 The suffix can be set to .nlog Profile for , This is saved in the program directory , If it is web In the project web.config Peer directory ( Program name .exe.nlog / web.nlog)
3 NLog.config This is a better form , Also recommended , Most of the Internet is in this form . It's in the program directory . 4 use NLog.dll.nlog
, This and NLog.dll In the same level directory I'm here app.config Configure database encryption string in ,NLog.config Automatically generated at install time , Simpler than other configurations
to configure targets and rules: <targets /> - Define the target of the log / output , Subordinate is <target> <rules /> -
Define routing rules for logs , Subordinate is <logger> Three Interpretation of labels <nlog> label autoReload Whether to allow automatic loading without restarting the program after modifying the configuration file
throwExceptions Exception thrown by internal log system internalLogLevel
Optional Trace|Debug|Info|Warn|Error|Fatal Determine the level of internal logs Off close internalLogFile
Write the internal debugging and exception information into the specified file proposal throwExceptions The value of is set to “false”, In this way, the problems caused by logs will not cause the application to crash .
<targets> label <target /> Area defines the target or output of the log ,, Here you can set the file name and format as required , Output mode .
name: Customize this target 's name , Available rule Use in rules type: Define type , The officially available types are : Chainsaw|ColoredConsole
|Console
|Database|Debug|Debugger|EventLog|File|LogReceiverService|Mail|Memory|MethodCall|Network
|NLogViewer|Null |OutputDebugString|PerfCounter|Trace|WebService But it's still common File
\Database \Colored Console\ Mail
layouts Used to specify layout style , grammar “${ attribute }”, You can insert context information into the log , See the appendix at the end of the paper for the available attributes provided by the government

<rules> label
Various rules are configured in logger in name - Name of recorder minlevel - Minimum level maxlevel - highest level level - Single log level
levels - A series of log levels , Separated by commas . writeTo - A list of targets that the log should be written to when matching rules , Separated by commas . <variable> label Variable definition
<variable name="variable1" value="${basedir}/logs"/> <targets> <target
name="File" xsi:type="File" fileName="${variable1}/${shortdate}.txt"/>
</targets> Four Call method 1 Add reference using NLog; 2 instantiation private Logger logger =
LogManager.GetCurrentClassLogger(); 3 call :   logger.Trace("Trace Message");
        logger.Debug("Debug Message");         logger.Info("Info Message");
        logger.Error("Error Message");         logger.Fatal("Fatal Message");
Five Examples of effects 1 Configuration output to file : <?xml version="1.0" encoding="utf-8" ?> <nlog
xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true" throwExceptions="false" internalLogLevel="Off"
internalLogFile="c:\temp\nlog-internal.log"> <variable name="variable1"
value="${newline}date: ${date}${newline}level: ${level}${newline}logger:
${logger}${newline}machinename: ${machinename} ${newline}message:
${message}${newline}appdomain: ${appdomain}${newline}assembly-version:
${assembly-version}${newline}basedir: ${basedir} ${newline}callsite:
${callsite}${newline}callsite-linenumber:
${callsite-linenumber}${newline}counter: ${counter}${newline}nlogdir:
${nlogdir} ${newline}processid: ${processid}${newline}processname:
${processname}${newline}specialfolder: ${specialfolder} ${newline}stacktrace:
${stacktrace}${newline}-----------------------------------------------------------"
/> <targets> <target name="log_file" xsi:type="File"
fileName="${basedir}/LogInformation/${level}_${shortdate}.txt"
layout="${variable1}" /> </targets> <rules> <logger name="*" writeTo="log_file"
/> </rules> </nlog> effect :




  appendix :layouts attribute


${activityid} Put it in the log System.Diagnostics trace 
${all-event-properties} Event log context
${appdomain} Current application domain
${assembly-version} application program
${basedir} Base directory for application domain .
${callsite} ( Class name , Source information for method name and related information ).
${callsite-linenumber} Calling the
${counter} numerical value
${date} Current date and time .
${document-uri} be used for Silverlight application .
${environment} environment variable
${event-properties}  
${exception} exception information
${file-contents} Display the contents of the specified file
${gc} garbage collector
${gdc} Diagnostic context
${guid} GUID
${identity} Thread identification information
${install-context} Installation parameters
${level} level .
${literal}  
${log4jxmlevent} XML Event description
${logger} Name of recorder
${longdate} Format classification of date and time yyyy-MM-dd HH:mm:ss.ffff.
${machinename} name
${mdc}  Mapping diagnostics
${mdlc} Asynchronous mapping diagnostic context
${message} news
${ndc} Thread structure
${ndlc} Asynchronous thread
${newline} Wrap text
${nlogdir} nlog.dll catalog .
${performancecounter} Performance counter .
${processid} Current process identifier
${processinfo} Operation information
${processname} The name of the current process .
${processtime} Format during this time HH:MM:ss.mmm.
${qpc} High precision timer , Based on the returned value from queryperformancecounter( optionally ) Convert to seconds .
${registry} From values in the registry .
${sequenceid} ID
${shortdate} short time   format YYYY-MM-DD.
${sl-appinfo} Silverlight application .
${specialfolder} Folder path
${stacktrace} - Stack trace renderer .
${tempdir} In temporary directory .
${threadid} Identifier of the current thread .
${threadname} Current thread .
${ticks} Current date and time .
${time} 24 Hour format HH:MM:ss.mmm.
${var} {$var}- Provide new variables (4.1)
${windows-identity} indows Thread identification information ( user name )
Official reference :
https://github.com/nlog/NLog/wiki/Layout%20Renderers
https://github.com/nlog/NLog/wiki/Targets
https://github.com/NLog/NLog/releases/
http://nlog-project.org/