Log4net configuration (logging) of ASP. NET MVC Learning

Source: Internet
Author: User
Tags log4net

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

Related Article

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.