Rollingfileappender Z in the Log4net

Source: Internet
Author: User

The application of the Log4 log component is really simple and practical, and after comparing the log functions of the Enterprise Library and LOG4, the individual feels that the function of LOG4 is more powerful. Under Supplemental instructions, the Enterprise Library I am using is version 2.0, and Log4net is version 1.2.1.

In the application of log4net, someone has done a description of the analysis, in this paper, the main analysis of the log4net in the application of Rollingfileappender.

The main reference is Log4net's help documentation. Under the Log4net-1.2.10/doc file in the project.


Rollingfileappender is mainly based on Fileappender, many of the attribute methods are inherited from Fileappender, so there are many of the same options set.

The following example makes the log file name Log.txt, the log filename is transformed by the file size, if the old log file size to 100KB, it will transform the file name Log.txt.1,log.txt.2,log.txt.3 ..., until 10

  1. <appender name="Rollingfileappender" type= "log4net. Appender.rollingfileappender ">
  2. <!--log file name --
  3. <file value="Log.txt"/>
  4. < Whether!--append to file
  5. <appendtofile value="true"/>
  6. <!--transform log files by file size--
  7. <rollingstyle value="Size"/>
  8. <!--maximum number of transformations --
  9. <maxsizerollbackups value="ten"/>
  10. <!--maximum file size --
  11. <maximumfilesize value="100KB"/>
  12. <!--log file name is static--
  13. <staticlogfilename value="true"/>
  14. <layout type="log4net. Layout.patternlayout ">
  15. <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline "/>
  16. </Layout>
  17. </appender>

The following example transforms the log file name by the time period, changes the log filename every minute, and the log file name is LOGFILEYYYYMMDD-HHMM, which, if you want to add the. log suffix after the file name, you must use the escape character, such as < Datepattern value= "Yyyymmdd-hhmm&quot; log&quot; "/>

  1. <appender name="Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
  2. <!--log file name begins --
  3. <file value="logfile" />
  4. < Whether!--append to file --
  5. <appendtofile value="true" />
  6. <!--transform in the form of date-
  7. <rollingstyle value="Date" />
  8. <!--date format--
  9. <datepattern value="yyyymmdd-hhmm" />
  10. <layout type="log4net. Layout.patternlayout ">
  11. <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline " />
  12. </Layout>
  13. </appender>

The following example combines the use of date and file size to transform the log file name.

  1. <appender name="Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
  2. <!--log file name begins --
  3. <file value="logfile" />
  4. < Whether!--append to file --
  5. <appendtofile value="true" />
  6. <!--mixed Use date and file size transform log file name --
  7. <rollingstyle value="Composite" />
  8. <!--date format--
  9. <datepattern value="yyyyMMdd" />
  10. <!--maximum number of transformations --
  11. <maxsizerollbackups value="ten" />
  12. <!--maximum file size --
  13. <maximumfilesize value="1MB" />
  14. <layout type="log4net. Layout.patternlayout ">
  15. <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline" />
  16. </Layout>
  17. </appender>

In the following example, each time the program runs, the original log file is backed up, and the log file name is changed when the log files are running larger than 50G.

  1. <appender name="Rollinglogfileappender" type= "log4net. Appender.rollingfileappender ">
  2. <!--log file name --
  3. <file value="logfile.txt" />
  4. <!--not append to original file --
  5. <appendtofile value="false" />
  6. <!--change file name to log file size --
  7. <rollingstyle value="Size" />
  8. <!--maximum number of transformations, 1 is unlimited--
  9. <maxsizerollbackups value="-1" />
  10. <!--maximum file size --
  11. <maximumfilesize value="50GB" />
  12. <layout type="log4net. Layout.patternlayout ">
  13. <conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline " />
  14. </Layout>
  15. </appender>

Below isRollingfileappenderPart of the classPublic Instance Properties(public property) description, not all tests, inevitably false, if you know, please do not mean to write.
countdirection: Defines whether the current file is the least numbered file or the largest number. The default is-1, always with a smaller number.
Datepattern:DateformatterDate format,stringType.
Encoding: Is the encoding of the file, the default isDefault, which is the code of the program itself.
Filterhead: The head of the separator.
Immediateflush: Indicates whether to export to file immediately, Boolean type.
Lockingmodel: File lock Type,Rollingfileappenderis not thread-safe in itself, and if there is no thread-safe limit in the program, you can configure it here to ensure the security of the write. There are two types:Fileappender.exclusivelockAnd Fileappender.minimallock
Name
: Sets the name of this appander.
SecurityContext: Less application, encrypt log only class, useSecuritycontextproviderTransformation. (The security requirements of the log is relatively high when should be able to apply it, LOG4 considered very thoughtful)
Threshold: Sets the content level, for example:DEBUG,INFOWait a minute.

There are two methods that are equivalent in the configuration of log4net:
<param name= "File" value= "Log.txt"/> and
<file value= "Log.txt"/>
Individuals are more inclined to the latter. There are a lot of param when it comes to applications on some databases.

Summary
LOG4 's application is much more than that, the Help file is also very detailed, both at the program level and at the configuration level have relevant examples. It took me a lot of work to tidy up a rollingfileappender . If you have access to other content, I will continue to sort it out.

Rollingfileappender Z in the 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.