Log4net (ii) record log to the document detailed

Source: Internet
Author: User

Original article, reprint must indicate source: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/log4net-%E4%BA%8C-%E4%B9%8B%E8%AE%B0% e5%bd%95%e6%97%a5%e5%bf%97%e5%88%b0%e6%96%87%e6%a1%a3%e8%af%a6%e8%a7%a3/

In the previous chapter, we briefly introduced the functions of log4net and a case of logging to a document.

This section will lead you to learn more about the meaning of each line of code in the previous case.

1 <Configuration>2   <configsections>3     < Sectionname= "Log4net"type= "System.Configuration.IgnoreSectionHandler"/>4   </configsections>5   <log4net>6     <Root>7       < Levelvalue= "All"/>8       <Appender-refref= "Fileappender"/>9     </Root>Ten <!--operations that are stored to a file - One     <Appendername= "Fileappender"type= "log4net." Appender.rollingfileappender ">     A       <paramname= "File"value= "Log\log.txt"/> -       <Rollingstylevalue= "Date"/> -       <paramname= "Appendtofile"value= "true"/> the       <!--<param name= "Datepattern" value= "yyyy-mm-dd". txt ""/> - - <paramname= "Datepattern"value= "Log.txt"/> -       <Layouttype= "log4net." Layout.patternlayout "> -         <paramname= "Conversionpattern"value= "%date%n---threadid=[%t] level=%-5p message=%m%n"/> +       </Layout> -     </Appender>    +   </log4net> A </Configuration>
View Code

The first is to configure Log4net.config

<Configuration></Configuration> node. This is the config file that must be cut to contain only one, to contain all the configuration content.

<configSections></configSections> node. This is used to add a custom node for the configuration file. Its content <section name= "log4net" type= "System.Configuration.IgnoreSectionHandler"/> For the sake of this configuration file to add a user-defined node, The name of the node is the log4net type. Ignoresectionhandler represents the type of node created. A simple introduction to this type is: Create a new configuration handler and add the specified configuration object to the section handler collection

<Root></Root> node. This node is the root log node, and its contents are the root log configuration, with a list of level and Appender values. All child user nodes (<logger>) are descendants, and the <logger> node is not used in the previous section code. Its content <level value= "All"/><appender-ref ref= "Fileappender"/> Represents the log level of the root node logging is a log of all levels recorded, <appender-ref The > tag defines the Appender object used by the log object. <appender-ref> declares a reference to a Appender object that is defined elsewhere.

the logs are classified into the following categories: ERROR, WARN, INFO, Debug,all
Error is a critical error, mainly a program
WARN for general warnings, such as session loss
Info is the general information to display, such as login log out
Debug information for the program

<appender name= "Fileappender" type= "log4net. Appender.rollingfileappender "> declares other information such as how to save your log4net log. In the code in the previous chapter, the log is saved in the "Write Log to file" type of all Appender is Rollingfileappender.

<Param> is the setting of Appender properties, such as saved file name, recording method, etc.

<Layout> output Log Formatter for displaying the last formatted output information to the user. Output information can be displayed in multiple formats

Format name meaning
The full name of the class to which the%c output log information belongs
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later, for example:%d{yyy-mm-ddhh:mm:ss}, output similar to: 2002-10-18-22:10:28
%f the class name of the class to which the output log information belongs
%l the occurrence of the output log event where the statement that outputs the log information is in the first line of the class it is in
%m the information specified in the output code, such as a message in log (message)
%n output a carriage return line break, Windows platform is "/r/n", UNIX platform is "/n"
%p output priority, or debug,info,warn,error,fatal. Debug if the debug () output is called, and so on
%r the number of milliseconds that the output from the application starts to output this log information
%t output The name of the thread that generated the log event

The second is to introduce log4net.config files into your project.

To introduce your project to Log4net, you have introduced the Log4net.config file you just configured into your project:

1 . Locate the AssemblyInfo.cs file in the project and add the code to the last line of the file:

" Log4net.config " true)]

2 . Add code to the Application_Start () function of the project

Log4net. Config.XmlConfigurator.Configure (new System.IO.FileInfo (Server.MapPath ("log4net.config ")));

Finally, the error log is logged successfully using Log4net in your code

After completing the above steps, you can declare the ILOG variable in your code to record the error.

ILog log = log4net. Logmanager.getlogger (Methodbase.getcurrentmethod (). declaringtype); log. Error ("The variable does not exist ");

Different log levels can be logged with different functions such as: Log. Error ("The variable does not exist"), it will generate a mistake of the same rank in your log file, same as log. info () generates an info log message log.debug ().

Log4net (ii) record log to the document detailed

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.