Nlog Simple Quick Tips

Source: Internet
Author: User

Not much nonsense to say directly into the topic.

1, in the project to add the application of Nlog

Two files will appear after installation

2. We open Nlog.config profile settings logging

<?XML version= "1.0" encoding= "Utf-8"?><Nlogxmlns= "Http://www.nlog-project.org/schemas/NLog.xsd"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance">  <!--See Https://github.com/nlog/nlog/wiki/Configuration-file For information on customizing logging rules and outputs   .  -  <!--define variable projectName project name logdirectory file path -  <variablename= "ProjectName"value= "Test"/>  <variablename= "Logdirectory"value= "F:/nlog/${projectname}/${shortdate}"/>  <Targets>    <!--define output Template: type= "file": This record is file type Filename= "${logdirectory}/all.log": Indicates output to file All.log layout= "...":         Error display format in output file ${logdirectory}: Path defined for ${longdate}: Output Long date yyyy-mm-dd HH:mm:ss.ffff (example: 2013-01-31 14:49:21.2120)        ${level}: Error level (from low to High Trace,debug,info,warn,error,fatal) ${newline}: Output a new line ${stacktrace}: output stack information ${callsite:classname=true:filename=true:includesourcepath=true:methodname=true}: Output namespace. class Name. method name (file path: line number) ${messag E}: Output error message -    <TargetXsi:type= "File"name= "LogFiles"FileName= "${logdirectory}/${shortdate}.log"Layout= "${longdate} ${level} ${message} ${stacktrace} ${callsite:classname=true:filename=true:includesourcepath=true : Methodname=true}${newline} " />  </Targets>  <rules>    <!--define output log: Name= "*": Record All information minlevel= "trace": the lowest error level recorded is Trace writeto= "logfiles": Log written to logfiles target In -    <Loggername="*"MinLevel= "Trace"WriteTo= "LogFiles" />  </rules></Nlog>

3. Test with a simple console program

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingNLog;namespacenlogdemo{classProgram {Private Static ReadOnlyLogger Logger =Logmanager.getcurrentclasslogger (); Static voidMain (string[] args)        {Writelog (); }         Public Static void() {Logger.Log (Loglevel.error,"Nlog Test Error"); }    }}

After the F5 run, there will be a time-named log file under the path I set

Log content

The contents of the log are related to the filename format that you configured in the configuration file

4, the above is the document records, the following we look at how the database records

<?XML version= "1.0" encoding= "Utf-8"?><Nlogxmlns= "Http://www.nlog-project.org/schemas/NLog.xsd"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance">  <!--See Https://github.com/nlog/nlog/wiki/Configuration-file For information on customizing logging rules and outputs   .  -  <!--define variable projectName project name logdirectory file path -  <variablename= "ProjectName"value= "Test"/>  <variablename= "Logdirectory"value= "F:/nlog/${projectname}/${shortdate}"/>  <Targets>    <!--define output to MySQL: type= "database": This record is dbprovider= "MySql.Data.MySqlClient": Using MYSQL Connection mode connect Ionstring= "": Connection string for database commandtext= "insert into Logs (createdate,loglevel,callsite,massage,stacktrace) VALUES (@Crea Tedate, @LogLevel, @CallSite, @Massage, @StackTrace) ": Insert statement <target xsi:type=" Database "name=" Logdatabase "DbPro Vider= "MySql.Data.MySqlClient" connectionstring= "server=127.0.0.1;database=mytestdatabase; Uid=root;                    pwd=123; " commandtext= "INSERT into Logs (create_time,loglevel,callsite,massage,stacktrace) VALUES (@create_time, @loglevel, @ Callsite, @massage, @stacktrace) "> <!--the value of the parameter corresponding to the INSERT statement -      <parametername= "Create_time"Layout= "${longdate}" />      <parametername= "LogLevel"Layout= "${level}" />      <parametername= "Callsite"Layout= "${callsite:classname=true:filename=true:includesourcepath=true:methodname=true}" />      <parametername= "Massage"Layout= "${message}" />      <parametername= "StackTrace"Layout= "${stacktrace}" />    </Target>  </Targets>  <rules>    <!--define output log: Name= "*": Record All information minlevel= "trace": the lowest error level recorded is Trace writeto= "logdatabase": Log written to database Logdataba In SE -    <Loggername="*"MinLevel= "Trace"WriteTo= "Logdatabase" />  </rules></Nlog>

SQL statements

CREATE TABLE' mytestdatabase '. ' Logs ' (' ID ')INT( One) not NULLauto_increment, ' Create_time 'DATETIME  not NULL, ' loglevel 'VARCHAR(5) not NULL, ' Callsite 'VARCHAR( the)DEFAULT NULL, ' massage ' longtext, ' StackTrace 'VARCHAR( the)DEFAULT NULL,  PRIMARY KEY(' ID ')) ENGINE=INNODB auto_increment=Ten DEFAULTCHARSET=UTF8;

Console program or the above code directly F5 run

Data in a database table

Of course <targets> The following can contain a lot of target we can also write log files to both the file and the database table

<?XML version= "1.0" encoding= "Utf-8"?><Nlogxmlns= "Http://www.nlog-project.org/schemas/NLog.xsd"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance">  <!--See Https://github.com/nlog/nlog/wiki/Configuration-file For information on customizing logging rules and outputs   .  -  <!--define variable projectName project name logdirectory file path -  <variablename= "ProjectName"value= "Test"/>  <variablename= "Logdirectory"value= "F:/nlog/${projectname}/${shortdate}"/>  <Targets>    <TargetXsi:type= "File"name= "LogFiles"FileName= "${logdirectory}/${shortdate}.log"Layout= "${longdate} ${level} ${message} ${stacktrace} ${callsite:classname=true:filename=true:includesourcepath=true : Methodname=true}${newline} " />    <TargetXsi:type= "Database"name= "Logdatabase"Dbprovider= "MySql.Data.MySqlClient"connectionString= "server=127.0.0.1;database=mytestdatabase; Uid=root; pwd=123; "CommandText= "INSERT into Logs (create_time,loglevel,callsite,massage,stacktrace) VALUES (@create_time, @loglevel, @callsite, @massage, @stacktrace) ">      <parametername= "Create_time"Layout= "${longdate}" />      <parametername= "LogLevel"Layout= "${level}" />      <parametername= "Callsite"Layout= "${callsite:classname=true:filename=true:includesourcepath=true:methodname=true}" />      <parametername= "Massage"Layout= "${message}" />      <parametername= "StackTrace"Layout= "${stacktrace}" />    </Target>  </Targets>  <rules>    <Loggername="*"MinLevel= "Trace"WriteTo= "Logfiles,logdatabase" />  </rules></Nlog>

This is just nlog daily simple application, more detailed application please refer

NLog Doc http://www.nlog-project.org/

Nlog Simple Quick Tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.