Log4net using notes in WinForm

Source: Internet
Author: User
Tags log4net

Add the app. Config, and add the following:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <configsections>    < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler,log4net "/>  </configsections>  <log4net>    <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">      <paramname= "File"value= "Log\log.txt" />      <paramname= "Appendtofile"value= "true" />      <paramname= "Maxsizerollbackups"value= "+" />      <paramname= "maximumFileSize"value= "2MB" />      <paramname= "Rollingstyle"value= "Size" />      <paramname= "Staticlogfilename"value= "true" />      <Layouttype= "log4net." Layout.patternlayout ">        <paramname= "Conversionpattern"value= "%-5p%d [%c]%m%n" />      </Layout>    </Appender>    <Root>      < Levelvalue= "All" />      <Appender-refref= "Rollinglogfileappender" />    </Root>  </log4net></Configuration>

If you need to add additional nodes to the App. Config, ensure that configsections is at the top, otherwise you will have the following error message:

< configsections > < Configuration The first child element of a > element.

The Program.cs file looks like this:

Static classProgram {StaticLog4net. ILog LOG =log4net. Logmanager.getlogger (Methodbase.getcurrentmethod ().        DeclaringType); /// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {//Catching unhandled exceptionsApplication.setunhandledexceptionmode (unhandledexceptionmode.catchexception); Application.ThreadException+=NewSystem.Threading.ThreadExceptionEventHandler (application_threadexception); AppDomain.CurrentDomain.UnhandledException+=NewUnhandledexceptioneventhandler (currentdomain_unhandledexception);            Application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (NewFormMain ()); }        Static voidApplication_threadexception (Objectsender, System.Threading.ThreadExceptionEventArgs e) {LOG.            Error (e.exception); //throw new Exception ("Thread Unknown Exception", e.exception);MessageBox.Show (E.exception.message,"Thread Exception", MessageBoxButtons.OK, Messageboxicon.error);        Application.exit (); }        Static voidCurrentdomain_unhandledexception (Objectsender, UnhandledExceptionEventArgs e) {Exception ex= E.exceptionobject asException; LOG.            Error (ex); MessageBox.Show (ex. Message,"Application Exceptions", MessageBoxButtons.OK, Messageboxicon.error);        Application.exit (); }    }

Log4net using notes in WinForm

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.