log4net Configuration Note----
First, add a reference to the Log4net.dll.
Under the Configuration node under the Web. config file, add the config information for log4net:
1 <!--log4net Configuration Node -2 <configsections>3 < Sectionname= "Log4net"type= "log4net." Config.log4netconfigurationsectionhandler, log4net "/>4 </configsections>5 <log4net>6 <!--Define some output appenders -7 <Appendername= "Rollinglogfileappender"type= "log4net." Appender.rollingfileappender ">8 <filevalue= "Log.txt"/>9 <Appendtofilevalue= "true"/>Ten <maxsizerollbackupsvalue= "Ten"/> One <maximumFileSizevalue= "10240KB"/> A <Rollingstylevalue= "Size"/> - <Staticlogfilenamevalue= "true"/> - <Layouttype= "log4net." Layout.patternlayout "> the <Conversionpatternvalue= "%date [%thread]%-5level%logger-%message%newline"/> - </Layout> - </Appender> - <Root> + < Levelvalue= "DEBUG"/> - <Appender-refref= "Rollinglogfileappender"/> + </Root> A </log4net> at <!--log4net Configuration node End -
Add a class MyExceptionFilterAttribute.cs:
1 Public classMyexceptionfilterattribute:handleerrorattribute2 {3 4 Public Override voidonexception (exceptioncontext filtercontext)5 {6 Base. Onexception (filtercontext);7 8 //handle the error message. Jump to an error page9 Loghelper.writelog (filterContext.Exception.ToString ());Ten //page jumps to error page OneFilterContext.HttpContext.Response.Redirect ("/shared/error"); A } -}
Add LogHelper.cs to the common class library and add the log write function:
1 Private StaticILog log = Logmanager.getlogger ("Loghelper");2 Public Static voidWritelog (stringerrormsg)3 {4Log. Error ("\ n----------------Error started-------------------\ n"+errormsg+"\ n----------------end of error-------------------\ n");5}
Add the following code to the Application_Start () method of the Global.asax file:
1 // Let the log4net configuration node function and register our filters 2 log4net. Config.XmlConfigurator.Configure (); 3 GlobalFilters.Filters.Add (new Myexceptionfilterattribute ());
Everything is done!
Log4net configuration (logging) of ASP. NET MVC Learning