Nlog is a class library written on the. NET platform, and we can use Nlog to add extremely sophisticated trace debugging code to the application.
Nlog allows us to customize the rules (rules)from the source of the trace message to the target that records the tracking information. The target for recording tracking information can be in the following ways:
- File
- Text Control console
- Email
- Database
- Other computers in the network (via TCP or UDP)
- MSMQ-based Message Queuing
- Windows system logs
First step: Install Nlog, download and install Nlog through the NuGet program controller.
Note: When searching for Nlog, there will be two. Install the Nlog first, then install the Nlog Configuration. Several files are then automatically added. Such as:
Step Two: Configure the Nlog.config file. The code is as follows:
<?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="MyVar"Value="myvalue"/> <!--bug level settings: Trace<<debug<<info<<warn<<error<<fatal-<!--set time code: ${ Date:format=yyyy-mm-dd Hh\:mm\:ss}-<targets> <target xsi:type="Console"Name="Console"layout="[${date:format=yyyy-mm-dd Hh\:mm\:ss}][${level}] ${message} ${exception}"/> <target xsi:type="File"Filename="${date:format=yyyy-mm-dd}.txt"Name="logfile"/> </targets> <!--Note: minlevel: The minimum level of the log; WriteTo: points to the corresponding target;final: rules under the same level, whether the rule is unique;--> <rules > <logger name="*"Minlevel="Trace"writeto="Console"Final="true"></logger> <logger name="*"Minlevel="Trace"writeto="logfile"></logger> </rules></nlog>
Configuration Information Development:
Logger label pair, control output range and level
Name property:
Specifies which code snippets to output information, for example: "<logger name=" somenamespace.component.* "...", outputting only the information printed in the somenamespace.component domain.
MinLevel Properties:
Specify the output level, Logging level is divided into the following level "Trace<<debug<<info<<warn<<error<<fatal", if we choose the Info value, The trace and debug level information is not output.
WriteTo Properties:
Specifies that the "target tag pair" is used to output information
Final property:
If this property is set to True, the message type that is output by the current logger label is no longer processed by the next logger label pair.
Target tag pair, output format, where to output
Name property:
The name of the target tag pair
Type property:
The type of target, such as target= "File". There are "Database", "Mail", "Network" and other types.
Silverlight uses file output to set the project to out of browser mode
FileName Property:
If the target type is file, you can specify the output file name.
Cases:
Filename= "File.txt",
Filename= "${basedir}/app_data/log.txt",
Filename= "${basedir}/log.txt",
FileName = "${specialfolder:mydocuments}/log.${shortdate}.txt"
Filename= "${logger}.txt"
Filename= "${shortdate}.txt"
Filename= "${windows-identity:domain=false}.txt
Filename= "${shortdate}/${windows-identity:domain=false}.txt"
If it is a Silverlight project, you can not specify the output file to the App_Data directory.
Layout properties:
Format output log information. Cases:
layout= "${date:format=hh\:mm\:ss}|${level}|${stacktrace}|${message}"
Layout= "[${date:format=yyyy-mm-ddhh\:mm\:ss}][${level}] ${message} ${exception}"
Layout= "${longdate}${callsite} ${level} ${message}"
Address property:
Specify which network server the log information is exported to, for example:
<targetname= "N1" xsi:type= "Network" address= "tcp://localhost:4001"/>
Step three: Code usage.
Private Static Logger Logger = Logmanager.getcurrentclasslogger (); Public Static void Main (string[] args) { logger. Error ("test1"); Logger. Trace (" write to file "); Console.readkey (); }
Effect Show:
Nlog Instructions for use