1. Introduction of Log4net.dll
1.1 NuGet Install or http://logging.apache.org/log4net/download log4net source code, compile Log4net.dll into the project.
2, Configuration Log4net.config
2.1 Adding configsections nodes in the Web. config file
< configsections > < name= "log4net" type= "Log4net". Config.log4netconfigurationsectionhandler, log4net "/> </ configsections>
2.2 Add log4net.config (set to always copy to output directory)
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <log4net> <!--Information Log Class - <Loggername= "Loginfo"> < Levelvalue= "All" /> <Appender-refref= "Infoappender" /> </Logger> <!--Information Log Additional media - <Appendername= "Infoappender"type= "log4net." Appender.rollingfileappender "> <!--what directory to export to - <paramname= "File"value= "log\\loginfo\\" /> <!--whether to overwrite the file - <paramname= "Appendtofile"value= "true" /> <!--number of backup files - <paramname= "Maxsizerollbackups"value= " the" /> <!--maximum size of a single log file - <paramname= "MaxFileSize"value= "10240" /> <!--whether to use a static file name - <paramname= "Staticlogfilename"value= "false" /> <!--log file name - <paramname= "Datepattern"value= "Yyyymmdd"". htm" " /> <paramname= "Rollingstyle"value= "Date" /> <!--layout, more parameters can be referred to the official website description - <Layouttype= "log4net." Layout.patternlayout "> <paramname= "Conversionpattern"value= "<hr color=red> %n Level:%-5p <BR> %n time:%d [%t] <BR> %n Description:%m <BR> "/> </Layout> </Appender> </log4net></Configuration>
2.3 Specify the configuration in the AssemblyInfo.cs file Log4net.config
" Log4net.config " " Config " true)]
3. Examples of code usage
3.1 Package Tool class Loghelper
namespace july.demo.win{ public class Loghelper { static readonly log4net. ILog loginfo = log4net. Logmanager.getlogger ( " ); public static void writelog ( info) { if (Loginfo. isinfoenabled) {Loginfo. info (info); } } }}
3.2 Use examples
Private void Form1_Load (object sender, EventArgs e) { loghelper.writelog (" form generation " );}
4. Operation result
4.1 as follows
(i) using log4net to generate log files