Goal:
By using the Log4net framework, you create an example program in a text file by date-sorted output logs.
Demand:
1 Use the log function provided by the LOG4NET framework for System log management.
2 The log of each day is stored with a corresponding log text. (log text names are distinguished by date)
Requirements Analysis:
1 Servicing Log4net.dll Component Library
2 because of the use of rollback file log, so Appender select: Rollingfileappender.
Rollback form: date.
The date format is initially set as: Yyyymmdd-hhmmm
Specific implementation:
(1) Create a console C # application
(2) Add Log4net.dll Reference
(3) Add a text file to the project, and the file name extension should read: Log.set, note: To set the property of this file to: if there is a newer copy
The contents of the file are:
<?xml version= "1.0" encoding= "Utf-8"?>
<log4net xmlns= "Urn:log4net" >
<root xmlns= "" >
<level value= "All"/>
<appender-ref ref= "Rollinglogfileappender"/>
</root>
<appender xmlns= "name=" Rollinglogfileappender "type=" log4net. Appender.rollingfileappender ">
<param name= "File" value= "Log\\log_"/>
<param name= "Appendtofile" value= "true"/>
<param name= "Staticlogfilename" value= "false"/>
<param name= "Datepattern" value= "Yyyymmdd". log" " />
<param name= "Rollingstyle" value= "Date"/>
<layout type= "log4net. Layout.patternlayout ">
<param name= "Conversionpattern" value= "%d [%t]%-5p%c [%x]-%m%n"/> <!--about here parameter settings-->
</layout>
</appender>
</log4net>
(4) Add in the AssembleInfo.cs file of the project: [Assembly:log4net. Config.xmlconfigurator (configfile = "Log.set", Watch = True)]
(5) coding
A. Add in your program file: using:log4net
B. Copy the following code in your main function
ILog log = log4net. Logmanager.getlogger (typeof (program));
Random Random = new Random ();
for (int i = 0; i < 2; i++)
{
Logging error logs
if (log. iserrorenabled)
Log. Debug ("You caused an error, the error ID is:" +random. Next (). ToString ());
Log a critical error
if (log. isfatalenabled)
Log. Fatal ("You raised a summary error that could cause the system to terminate, with the ID:" +random. Next (). ToString ());
Record General Information
if (log. isinfoenabled)
Log. info ("You plan to record a message, ID:" +random.) Next (). ToString ());
Logging Debug Information
if (log. isdebugenabled)
Log. Debug (debug information, Debug ID: + Random.) Next (). ToString ());
Logging warning messages
if (log. iswarnenabled)
{
Log. Warn ("WARNING: Warning ID is:" + random.) Next (). ToString ());
}
}
(6) test, run your console application every other day. Let's say I ran it once yesterday and ran it once today. Then a Log folder appears in the Bin\Debug directory of the program, and two text files "Log_20120427.log" and "log_20120428" appear in the folder. Open one of the files to see the log record.
Would like to upload the sample program, Rogue csdn is too bad. A sample download link is provided behind
(7) The following are several commonly used rollback log storage methods,
Collect
Rollingfileappender is built primarily on Fileappender, and many attribute methods are inherited from Fileappender, so many of them are the same on option settings.
The following example makes the log file name Log.txt, and the log file name is transformed by file size, and if the old log file size is 100KB, the file name is log.txt.1,log.txt.2,log.txt.3 ... until 10.
<appender name= "Rollingfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name-->
<file value= "Log.txt"/>
<!--append--> to File
<appendtofile value= "true"/>
<!--transform log files according to the size of the file-->
<rollingstyle value= "Size"/>
<!--maximum transformation quantity-->
<maxsizerollbackups value= "Ten"/>
<!--maximum file size-->
<maximumfilesize value= "100KB"/>
<!--whether the log file name is static-->
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following example transforms the log file name with the time period, changing the log filename every minute, and the log file name is LOGFILEYYYYMMDD-HHMM, which adds that if you want to add the. log suffix to the file name, you must use the escape character, such as < Datepattern value= "Yyyymmdd-hhmm" log" "/>
The
<appender name= "Rollinglogfileappender type=" log4net. Appender.rollingfileappender ">
<!--log file name--> &NBSP
<file value= "logfile"/>&NBSP;
<!--append to file-->
<appendtofile value= "true"/>
<!--transform in the form of a date-->
<rollingstyle value= "date"/>
<!--date format-->
<datepattern value= "yyyymmdd-hhmm"/>&NBSP;
<layout type= " Log4net. Layout.patternlayout ">
<conversionpattern value="%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline "/>&NBSP;
</layout> &NBSP
</appender>
The following example combines the use of date and file size to transform log file names.
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name opening-->
<file value= "logfile"/>
<!--append to file-->
<appendtofile value= "true"/>
<!--mixed Use date and file size transform log file name-->
<rollingstyle value= "Composite"/>
< format for!--dates-->
<datepattern value= "YyyyMMdd"/>
<!--maximum transformation quantity-->
<maxsizerollbackups value= "Ten"/>
<!--maximum file size-->
<maximumfilesize value= "1MB"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following example is a backup of the original log file every time the program runs, and the log file name is changed when the log file running is larger than 50G.
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name-->
<file value= "LogFile.txt"/>
<!--is not appended to the original file-->
<appendtofile value= "false"/>
<!--transform file names by log file size-->
<rollingstyle value= "Size"/>
<!--maximum number of transformations-1 is unrestricted-->
<maxsizerollbackups value= "-1"/>
<!--maximum file size-->
<maximumfilesize value= "50GB"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following is a description of part of the public Instance properties (Common properties) in the Rollingfileappender class, not all of which are inevitably fallacious, if you know, please don't be stingy to write.
Countdirection: Defines whether the current file is a minimum numbered file or a maximum number of files. The default is-1, always with a smaller number.
Datepattern:dateformatter date format, String type.
Encoding: Is the file encoding, default defaults, is the code of the program itself.
Filterhead: Separator head.
Immediateflush: Indicates whether to output immediately to the file, the Boolean type.
Lockingmodel: File lock type, Rollingfileappender itself is not thread-safe, and if there is no thread-safe restriction in the program, it can be configured here to ensure security at write time. There are two types: Fileappender.exclusivelock and Fileappender.minimallock.
Name: Sets the names of this appander.
SecurityContext: Relatively few applications, to encrypt the log only class, using Securitycontextprovider conversion. (The confidentiality of the log requirements of a higher time should be applied on it, LOG4 considered quite comprehensive)
Threshold: Set the level of content, such as: Debug,info and so on.
Rollingfileappender is built primarily on Fileappender, and many attribute methods are inherited from Fileappender, so many of them are the same on option settings.
The following example makes the log file name Log.txt, and the log file name is transformed by file size, and if the old log file size is 100KB, the file name is log.txt.1,log.txt.2,log.txt.3 ... until 10.
<appender name= "Rollingfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name-->
<file value= "Log.txt"/>
<!--append--> to File
<appendtofile value= "true"/>
<!--transform log files according to the size of the file-->
<rollingstyle value= "Size"/>
<!--maximum transformation quantity-->
<maxsizerollbackups value= "Ten"/>
<!--maximum file size-->
<maximumfilesize value= "100KB"/>
<!--whether the log file name is static-->
<staticlogfilename value= "true"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following example transforms the log file name with the time period, changing the log filename every minute, and the log file name is LOGFILEYYYYMMDD-HHMM, which adds that if you want to add the. log suffix to the file name, you must use the escape character, such as < Datepattern value= "Yyyymmdd-hhmm" log" "/>
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name opening-->
<file value= "logfile"/>
<!--append to file-->
<appendtofile value= "true"/>
The form of the <!--transformation is the date-->
<rollingstyle value= "Date"/>
< format for!--dates-->
<datepattern value= "Yyyymmdd-hhmm"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following example combines the use of date and file size to transform log file names.
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name opening-->
<file value= "logfile"/>
<!--append to file-->
<appendtofile value= "true"/>
<!--mixed Use date and file size transform log file name-->
<rollingstyle value= "Composite"/>
< format for!--dates-->
<datepattern value= "YyyyMMdd"/>
<!--maximum transformation quantity-->
<maxsizerollbackups value= "Ten"/>
<!--maximum file size-->
<maximumfilesize value= "1MB"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following example is a backup of the original log file every time the program runs, and the log file name is changed when the log file running is larger than 50G.
<appender name= "Rollinglogfileappender" type= "log4net". Appender.rollingfileappender ">
<!--log file name-->
<file value= "LogFile.txt"/>
<!--is not appended to the original file-->
<appendtofile value= "false"/>
<!--transform file names by log file size-->
<rollingstyle value= "Size"/>
<!--maximum number of transformations-1 is unrestricted-->
<maxsizerollbackups value= "-1"/>
<!--maximum file size-->
<maximumfilesize value= "50GB"/>
<layout type= "log4net. Layout.patternlayout ">
<conversionpattern value= "%date [%thread]%-5level%logger [%PROPERTY{NDC}]-%message%newline"/>
</layout>
</appender>
The following is a description of part of the public Instance properties (Common properties) in the Rollingfileappender class, not all of which are inevitably fallacious, if you know, please don't be stingy to write.
Countdirection: Defines whether the current file is a minimum numbered file or a maximum number of files. The default is-1, always with a smaller number.
Datepattern:dateformatter date format, String type.
Encoding: Is the file encoding, default defaults, is the code of the program itself.
Filterhead: Separator head.
Immediateflush: Indicates whether to output immediately to the file, the Boolean type.
Lockingmodel: File lock type, Rollingfileappender itself is not thread-safe, and if there is no thread-safe restriction in the program, it can be configured here to ensure security at write time. There are two types: Fileappender.exclusivelock and Fileappender.minimallock.
Name: Sets the names of this appander.
SecurityContext: Relatively few applications, to encrypt the log only class, using Securitycontextprovider conversion. (The confidentiality of the log requirements of a higher time should be applied on it, LOG4 considered quite comprehensive)
Threshold: Set the level of content, such as: Debug,info and so on.
I wanted to upload the sample program, Rogue csdn is too bad.