ASP. NET applications use nlog to record logs

Source: Internet
Author: User

In the previous company, nlog is used for log recording, which is convenient for calling and configuration, making it very convenient to view logs. So I thought of it in the new project of the new company. The background information is complete.

First check the configuration. in ASP. NET applications, nlog automatically scans the following files:

1) web. config
2) web. nlog files in the same directory as Web. config
3) The nlog. config file in the program directory
4) nlog. dll. nlog file in the directory where nlog. dll is located

We configure according to the first file, then the content in the web. config configuration file is as follows:

<Configsections>

<! -- Nlog -->

<Section name = "nlog" type = "nlog. config. configsectionhandler, nlog" restartonexternalchanges = "false"/>

</Configsections>

<Nlog>

<Include file = "$ {basedir}/configuration/nlog. config"/>

</Nlog>

Then nlog. config. The content of this file is known by name. It is the configuration information of the specific behavior of nlog. The example 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">
<Targets>
<Target name = "exceptionfile" xsi: type = "file" filename = "$ {basedir}/site_log/$ {latest date}/${logger=_${levelpolic.txt" layout = "$ {longdate} | $ {level: uppercase = true }|$ {logger }$ {newline }$ {message }$ {newline} "/>
<Target name = "console" xsi: TYPE = "coloredconsole" layout = "$ {Date: format = HH \: mm \: SS} | $ {level} | $ {stacktrace} | $ {message} "/>
</Targets>
<Rules>
<Logger name = "*" minlevel = "debug" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "info" writeto = "console"/>
<Logger name = "*" minlevel = "trace" writeto = "console"/>
<Logger name = "*" minlevel = "Fatal" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "error" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "Warn" writeto = "exceptionfile"/>
</Rules>
</Nlog>

The following describes how to configure nodes one by one.

Note that nlog configuration files are case sensitive.

<Nlog xmlns = "http://www.nlog-project.org/schemas/NLog.xsd" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance">

The namespace can be configured or not configured. If the namespace is configured, you can use vs smart sensing.

<Targets>
<Target name = "exceptionfile" xsi: type = "file" filename = "$ {basedir}/site_log/$ {latest date}/${logger=_${levelpolic.txt" layout = "$ {longdate} | $ {level: uppercase = true }|$ {logger }$ {newline }$ {message }$ {newline} "/>
<Target name = "console" xsi: TYPE = "coloredconsole" layout = "$ {Date: format = HH \: mm \: SS} | $ {level} | $ {stacktrace} | $ {message} "/>
</Targets>

Defines the output of each logging method. The example defines two output modes: file and console,

Name: node/output name

Xsi: Type: output type

Filename: Log File Name

Layout: This is an outstanding feature of nlog, layout function. The format is $ {}. Context information such as date, log record level, and caller can be defined in brackets. It can be either a standard naming file or a log for convenient analysis.

<Rules>
<Logger name = "*" minlevel = "debug" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "info" writeto = "console"/>
<Logger name = "*" minlevel = "trace" writeto = "console"/>
<Logger name = "*" minlevel = "Fatal" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "error" writeto = "exceptionfile"/>
<Logger name = "*" minlevel = "Warn" writeto = "exceptionfile"/>
</Rules>

Name: the name of the log. Wildcard characters can be used *

Minlevel: The minimum log level. If it is configured as debug, all logs higher than or equal to this level will be written to the log file. Of course, you can also specify maxlevel or single level, such as level = "debug", so only the debug level information is recorded. You can also specify levels in the format of levels = "debug, Info ".

Writeto: The target log file to be written.

There are so many configuration files. Next we should call the program and declare a public variable, as shown below:

Public static logger = logmanager. getcurrentclasslogger ();

Use

Logger. Info ("I'm eddypeng. Thank you for reading this! ")

 

Now, the simple application of nlog is here. I believe that this article will be sufficient if it is just a simple application. If you want to learn more deeply, you can go to the following two addresses:

Http://www.cnblogs.com/dflying/category/78087.html

Http://nlog-project.org/wiki

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.