C # using/configuring Log4net

Source: Internet
Author: User

1. First add NuGet packages to your project ...

2. Then search for log4net in the NuGet form, then click Install < Install the process may last for a few minutes, please be patient >

3. Add a config file to your project, if you already have an app. config, add content directly to it:

The XML code in the configuration is as follows:

  <log4net>    <Loggername= "Logerror">      < Levelvalue= "ERROR" />      <Appender-refref= "Errorappender" />    </Logger>    <Loggername= "Loginfo">      < Levelvalue= "INFO" />      <Appender-refref= "Infoappender" />    </Logger>    <Appendername= "Errorappender"type= "log4net." Appender.rollingfileappender ">      <paramname= "File"value= "Log\\logerror\\" />      <paramname= "Appendtofile"value= "true" />      <paramname= "Maxsizerollbackups"value= "+" />      <paramname= "MaxFileSize"value= "10240" />      <paramname= "Staticlogfilename"value= "false" />      <paramname= "Datepattern"value= "Yyyymmdd&quot;". txt&quot; " />      <paramname= "Rollingstyle"value= "Date" />      <Layouttype= "log4net." Layout.patternlayout ">        <paramname= "Conversionpattern"value= "%n exception time:%d%n exception level:%-5p%n exception content:%m%n" />      </Layout>      <!--&lt; &gt; = <>%n = Enter -    </Appender>    <Appendername= "Infoappender"type= "log4net." Appender.rollingfileappender ">      <paramname= "File"value= "log\\loginfo\\" />      <paramname= "Appendtofile"value= "true" />      <paramname= "MaxFileSize"value= "10240" />      <paramname= "Maxsizerollbackups"value= "+" />      <paramname= "Staticlogfilename"value= "false" />      <paramname= "Datepattern"value= "Yyyymmdd&quot;". txt&quot; " />      <paramname= "Rollingstyle"value= "Date" />      <Layouttype= "log4net." Layout.patternlayout ">        <paramname= "Conversionpattern"value= "Log time:%d%n log level:%-5p%n Log content:%m%n%n" />      </Layout>    </Appender>  </log4net>

Finally, register the config file in the project's AssemblyInfo.cs file as follows:

Register the Log4net.config configuration file for the project [Assembly:log4net. Config.xmlconfigurator (configfileextension = "config", Watch = True)]

Finally, add the log class:

     Public classLoghelper {PrivateLoghelper () {} Public Static ReadOnlyLog4net. ILog loginfo = log4net. Logmanager.getlogger ("Loginfo");  Public Static ReadOnlyLog4net. ILog logerror = log4net. Logmanager.getlogger ("Logerror");  Public Static voidSetconfig () {log4net.        Config.XmlConfigurator.Configure (); }         Public Static voidsetconfig (FileInfo configfile) {log4net.        Config.XmlConfigurator.Configure (ConfigFile); }         Public Static voidWritelog (stringinfo) {            if(Loginfo. isinfoenabled) {Loginfo.            info (info); }        }         Public Static voidWritelog (stringinfo, Exception se) {            if(Logerror. iserrorenabled) {Logerror.            Error (info, SE); }        }    }

Well, when we get here, the preparation is done, so let's start the test.

The log file is logged by default in the root directory and the path can be changed in config

C # using/configuring 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.