NLog Is a simple and flexible.NET Logging class library,NLog OfAPI Very similar tolog4net
, And the configuration is very simple. through the use ofNLog, 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, butlog4net But using hierarchicalappender To configure, This allowsNLog 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 Acquisition mode 1 Get this class: First kind staynuget Console input installationnlog command:
Install-Package NLog.Config
Second kinds staynuget On the package management interface, Search installationNlog andNlog
configuration, This will automatically generate aconfig file, This and the program orweb Ownconfig Use management in the future, Mutual influence
Third kinds GitHub upper Two configuration file Support multiple configuration forms:
1 Directly using the application profile orweb 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 isweb In the projectweb.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 UseNLog.dll.nlog
, This sumNLog.dll In the same level directory I am Configure database encryption string in,NLog.config Automatically generated at install time , Simpler than other configurations
To configuretargets andrules: <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
OptionalTrace|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 proposalthrowExceptions Set the value 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 thistarget Name, Availablerule Use in rules type: Definition type, The officially available types are: Chainsaw|ColoredConsole
|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 inlogger in name - Name of recorder minlevel - Lowest 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 Calling 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 Example of effect 1 Configuration output to file: <?xml version="1.0" encoding="utf-8" ?> <nlog
xsi:schemaLocation=" 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-linenumber}${newline}counter: ${counter}${newline}nlogdir:
${nlogdir} ${newline}processid: ${processid}${newline}processname:
${processname}${newline}specialfolder: ${specialfolder} ${newline}stacktrace:
/> <targets> <target name="log_file" xsi:type="File"
layout="${variable1}" /> </targets> <rules> <logger name="*" writeTo="log_file"
/> </rules> </nlog> Effect:

  appendix:layouts attribute

${activityid} Put it in the logSystem.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 class
${counter} numerical value
${date} Current date and time.
${document-uri} Be used forSilverlight application.
${environment} environment variable
${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.
${log4jxmlevent} XML Event description
${logger} Name of recorder
${longdate} Format classification of date and timeyyyy-MM-dd HH:mm:ss.ffff.
${machinename} Name
${mdc}  Mapping diagnosis
${mdlc} Asynchronous mapping diagnostic context
${message} news
${ndc} Thread structure
${ndlc} Asynchronous thread
${newline} Word wrap
${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 timeHH:MM:ss.mmm.
${qpc} High precision timer, Based on the returned value fromqueryperformancecounter( optionally) Convert to seconds.
${registry} From values in the registry.
${sequenceid} ID
${shortdate} short time  formatYYYY-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 formatHH:MM:ss.mmm.
${var} {$var}- Provide new variables(4.1)
${windows-identity} indows Thread identification information( User name)
Official reference: