(i) using log4net to generate log files

Source: Internet
Author: User
Tags log4net

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&quot;". htm&quot; " />      <paramname= "Rollingstyle"value= "Date" />      <!--layout, more parameters can be referred to the official website description -      <Layouttype= "log4net." Layout.patternlayout ">        <paramname= "Conversionpattern"value= "&lt;hr color=red&gt;          %n Level:%-5p &lt;BR&gt;          %n time:%d [%t] &lt;BR&gt; %n Description:%m &lt;BR&gt; "/>      </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

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.