NLog,

an

NLog is a simple and flexible one. NET logging class library,NLog API is very similar to Log4NET, and the configuration is very simple. By using NLog, we can do it in any kind of. NET language output debugging information with context, according to the project needs to configure the format and output target rules.

While NLog is configured using routing, Log4net uses a hierarchical appender configuration that makes the NLog configuration file easy to read and maintain in the future.

Supports various types of log output: text file system logs Database console email

access

1 Obtaining this class:

First, enter the Install nlog command on the Nuget console: install-package nlog.config

The second option is to search for installation Nlog and Nlog Configuration on the Nuget package management interface. This automatically generates a config file, which is easily separated from the application or the Web’s own config file for future use and management

The third lot on https://github.com/NLog/NLog/ https://github.com/NLog/NLog/releases/

The configuration file

Supports multiple configuration modes:

1 Directly use the application configuration file or web configuration file (app.config/web.config)

Nlog. This is stored in the same directory as web.config (exe. Nlog/web.nlog) in a Web project.

3 nlog. config this is a better form, and it is recommended to use, most of the web use this form. It’s in the program directory.

4 Use nlog.dll. NLog. This directory is in the same level as nlog.dll

Here I configure the database encryption string in app.config. Nlog. config is automatically generated during installation, which is easier than other configurations

Configure targets and Rules

– Defines the target/output of the log, subordinate to

– Defines the routing rules for logs

Tag reading

Nlog label

AutoReload Indicates whether automatic loading is allowed after the configuration file is changed. ThrowExceptions The internal log system throws an exception Optional Trace | Debug | Info | Warn | Error | Fatal decision level of internal log Off closed internalLogFile put internal debugging and exception information written proposal throwExceptions value for the specified file is set to "false", This prevents problems caused by logging from crashing the application.Copy the code

The targets TAB

The area defines the target or output of the log, where you can set the file name and format, and output mode as required.

Name: Specifies the name of the target, which can be used in the rule

Type: Defines the type. The available types are as follows:

Chainsaw|ColoredConsole |Console |Database|Debug|Debugger|EventLog|File|LogReceiverService|Mail|Memory|MethodCall|Network |NLogViewer|Null |OutputDebugString|PerfCounter|Trace|WebService
Copy the code

File \Database \Colored Console\ Mail\Network

You can also use the ${attribute} syntax to insert context information into the log. You can also use the ${attribute} syntax to insert context information into the log

Rules label

The various rules are configured in the logger child tag

Name - The name of the logger minLevel - Lowest level maxLevel - Highest level level - Single log level Levels - A series of log levels, separated by commas. WriteTo - A list of targets to which the log should be written when a rule matches, separated by commas.Copy the code

Variable label

Variable definitions

 <variable name="variable1" value="${basedir}/logs"/> 
 <targets>   
    <target name="File" xsi:type="File" fileName="${variable1}/${shortdate}.txt"/>  
 </targets>
Copy the code

A method is called

1 add a reference using NLog;

2 instantiation private Logger Logger. = that the LogManager GetCurrentClassLogger ();

3 call:

 logger.Trace("Trace Message");
 logger.Debug("Debug Message");
 logger.Info("Info Message");
 logger.Error("Error Message");
 logger.Fatal("Fatal Message");
Copy the code

5. Examples of effects

1 Configure output to file:

 
      
<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: ${date}${newline}level: ${level}${newline} Logger: ${logger}${newline}machinename: ${machinename} ${newline}message: ${message}${newline} appDomain: ${appDomain}${newline}assembly-version: ${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>
Copy the code

Effect:

Appendix: Layouts attribute

${activityid} Place it in the log System.Diagnostics Trace
${all-event-properties} Event log context
${appdomain} Current application domain
${assembly-version} The application
${basedir} The base directory of the application domain.
${callsite} (Source information for class name, method name, and related information).
${callsite-linenumber} The calling class
${counter} The numerical
${date} Current date and time.
${document-uri} For Silverlight applications.
${environment} The environment variable
${event-properties}
${exception} The exception information
${file-contents} Displays 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} Logger name
${longdate} The date and time format is YYYY-MM-DD HH: MM: SS.FFFF.
${machinename} The name of the
${mdc} Mapping the diagnosis
${mdlc} Asynchronous mapping diagnostic context
${message} The message
${ndc} Thread structure
${ndlc} Asynchronous thread
${newline} Wrap text
${nlogdir} Nlog. DLL directory.
${performancecounter} The performance counter.
${processid} Current process identifier
${processinfo} Run the information
${processname} The name of the current process.
${processtime} The time process format is HH:MM:ss.mmm.
${qpc} A high-precision timer that converts (optionally) from query Performance Ounter to seconds based on the returned value.
${registry} Values from the registry.
${sequenceid} ID
${shortdate} The short-time format is YYYY-MM-DD.
${sl-appinfo} Silverlight application.
${specialfolder} Folder path
${stacktrace} – Stack trace renderer.
${tempdir} Temporary directory.
${threadid} The identifier of the current thread.
${threadname} Current thread.
${ticks} Current date and time.
${time} The 24-hour format is HH:MM:ss.mmm.
${var} {$var}- Provide new variables (4.1)
${windows-identity} Indows thread identity information (user name)

Official reference: github.com/nlog/NLog/w… https://github.com/nlog/NLog/wiki/Targets github.com/NLog/NLog/r… http://nlog-project.org/