Applying the Log4net (c) in C # code to the interpretation of a configuration file in Log4net

Source: Internet
Author: User

<log4net> <!--error Log class--<logger Name= "Logerror" > <level value= "All"/> <appender-ref re f= "Errorappender"/> </logger> <!--Information Log class-<logger name= "Loginfo" > <level value= "All"/&G    T <appender-ref ref= "Infoappender"/> </logger> <!--error log additional media--<appender name= "Errorappender" Typ E= "Log4net. Appender.rollingfileappender "> <param name=" File "value=" log\\logerror\\ "/> <param name=" AppendToFile " Value= "true"/> <param name= "maxsizerollbackups" value= "10240"/> <param name= "MaxFileSize" value= /> <param name= "Staticlogfilename" value= "false"/> <param name= "Datepattern" value= "YyyyMMdd&quot;. htm&quot; "/> <param name=" Rollingstyle "value=" Date "/> <!--layout--<layout type=" log4net. Layout.patternlayout "> <param name=" Conversionpattern "value=" &lt;hr color=red&gt;%n exception Time:%d [%t] & Lt;br&gT;%n exception Level:%-5p &lt;br&gt;%n Exception class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%n &lt;hr Size=1&gt; " /> </layout> </appender> <!--Infolog Additional media--<appender name= "Infoappender" type= "log4net. Appender.rollingfileappender "> <param name=" File "value=" log\\loginfo\\ "/> <param name=" AppendToFile "V Alue= "true"/> <param name= "maxfilesize" value= "10240"/> <param name= "maxsizerollbackups" value= "100"/ > <param name= "Staticlogfilename" value= "false"/> <param name= "Datepattern" value= "Yyyymmdd&quot;. htm&quot; "/> <param name=" Rollingstyle "value=" Date "/> <!--Infolog layout--<layout type=" log4n Et. Layout.patternlayout "> <param name=" Conversionpattern "value=" &lt;hr color=blue&gt;%n Log Time:%d [%t] &amp ; lt;br&gt;%n log level:%-5p &lt;br&gt;%n Log class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%n &lt;HR  Size=1&gt; " /> </layout&Gt </appender></log4net>

First of all, do not analyze what the above configuration information means. Let's think about what our log class is used to do, assuming we have an inventory management system, divided into two modules, one for the library and one for the storage. We may want to store the information about the library in a folder, the information stored in another folder. In this way, it is common practice to write the logs to different locations using different paths in the log class, but what if we don't just want to write to the log file and want to insert the log files into the database? In general, we will set up some more methods to write to the database. In addition, the layout of the normal log information and error log information, we may want to change the layout of the information according to the type of log, such as the error log contains abnormal information records, there is no abnormal record in the normal log.

In summary, the requirements of our log class are usually: 1, can be requested to write to different media (file, database, mail, etc.); 2, can be based on the different types of log, write to a different location; 3, can change the layout of the log according to the type of information.

Have to say, want to write a log record class is not easy, see is a simple need to do well, but also need a big effort. And these in the log4net, only need to pass some configuration, can complete. If you do not bother to write configuration information, you can also directly copy the above configuration information used.

Explanation of the nodes in the configuration file

Logger is the logger responsible for logging, assuming that we need to record some normal runtime logs and error log when the exception occurs, we can add two logger implementations in the configuration file.
Appender provides the recorded media, as mentioned earlier, we may want to record data to the file and database, we can simply write the Appender implementation, and Log4net has already provided some common appender, We can simply modify some of the configuration files to implement simultaneous writes to the database and simultaneously to the file.

Layout is responsible for formatting the contents of the entry. In fact, decide what the log file will look like.

Explanation of the Logger node

Explain the error log class as an example

<!--error log class  -<logger name= "Logerror" >              <!--the name of the log class--    <level value= "All"/>               <!--Define the log level of the record--    <appender-ref ref= "Errorappender"/>< What media is recorded!----  </logger >

level defines the log levels of records, that is, what level of logs you want to log, and the level from high to low is:

None
Fatal
ERROR
WARN
DEBUG
INFO
All

Level definition be aware that if you define debug, then the information below the debug level will not be logged, what do you mean? That is, even if you write a log message in the program with Log.info (), you specify the level in the configuration as debug, Because the info level is lower than debug, it is not logged. This is a very flexible process.

Appender-ref defines what media the log is to be written to. The above example is written to errorappender this media, in the Errorappender node we can define the log related to where to write, the format of the log file is what information.

Explanation of the Appender node

Taking Errorappender as an example

<!--error log attach media--<appender name= "Errorappender" type= "log4net. Appender.rollingfileappender "> <!--Name property to specify its names, type is log4net. The name of a class in the Appender namespace, meaning, specify which media to use--<param name= "File" value= "log\\logerror\\"/> <!--output to what directory--&lt ;p Aram Name= "Appendtofile" value= "true"/><!--overwrite to file--<param name= "maxsizerollbackups" value= "100"/& gt;<!--Number of backup files--<param name= "MaxFileSize" value= "10MB"/><!--single log file maximum size--<param name= "St Aticlogfilename "value=" false "/><!--whether to use a static file name--<param name=" Datepattern "value=" Yyyymmdd&quot;. htm&quot; "/><!--log file name--<param name=" Rollingstyle "value=" Date "/> <!--layout--<layo UT type= "log4net. Layout.patternlayout "> <param name=" Conversionpattern "value=" &lt;hr color=red&gt;%n exception Time:%d [%t] & Lt;br&gt;%n exception Level:%-5p &lt;br&gt;%n Exception class:%c [%x] &lt;br&gt;%n%m &lt;br&gt;%n &lt;HR SiZe=1&gt; " /> </layout> </appender>

In the appender node, type=rollingfileappender means to write the log to a file in the form of a rollback file.

The file node specifies what directory the files are to be written to, and the "log\\logerror\\" representation in the previous example is written to the program Input directory (debug directory) \log\logerror\ folder.

The appendtofile node Specifies whether to overwrite the file. Suppose we already have a 20131028.htm log file, and when True, the log file is appended to the file. When false, Log4net will first back up the original log file and generate a new log file (see).

maxfilesize the largest file size. We can use "KB", "MB" or "GB" as the suffix to maxfilesize the size. The default file size is 10MB.

Rollingstyle is the way files are created. The previous example is set to create a new file in Date mode.

datepattern Date format, when we set Rollingstyle to date, Log4net automatically uses the date format in Datepattern to create a new log file.

maxsizerollbackups This property is used to set the log file to the maxfilesize size, the backup file is created automatically. The number of backup files is determined by maxsizerollbackups . For example, we are using the date format as the log file name, assuming today is 2013-10-28, then the log file name created today is 20131028.htm, when the file is more than maxfilesize , Log4net automatically renamed the old 20131028.htm to 20131028.htm.1 and created a new 20131028.htm file.

Staticlogfilename Whether a static file name is used. Because our example is using date as the file name, the daily log file name is dynamic, so the above example is false. If a static file name is used, then the name of the log file is uniquely determined. You can refer to the following configuration file for Setup. In the following configuration file, the static file name is used, and the resulting log file name is Log.txt.

<appender name= "Rollingfileappender" type= "log4net. Appender.rollingfileappender ">    <file value=" Log.txt "/>    <appendtofile value=" true "/>    <rollingstyle value= "Size"/>    <maxsizerollbackups value= "ten"/> <maximumfilesize    value= " 100KB "/>    <staticlogfilename value=" true "/>    <layout type=" log4net. Layout.patternlayout ">        <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-% Message%newline "/>    </layout></appender>

layout is layouts. The conversionpattern in Layout is the format of the log file, and some symbols are appended below. Due to the format of the way more, in addition to the following, you can also see their own official documents.

%m[%message] Log Messages for output
%n Line break
%d[%datetime] Outputs the moment at which the current statement runs
%r The number of milliseconds that the output program consumes from running to executing to the current statement
%d The thread ID where the current statement is located
%p Current priority level of the log
%c The name of the current log object
%l The line number where the output statement is located
%F The file name where the output statement resides
%-Digital Indicates the minimum length of the item and, if not enough, fills it with a space

Applying the Log4net (c) in C # code to the interpretation of a configuration file in Log4net

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.