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