Catch exceptions in ASP. NET MVC and use the Log4net logging component

Source: Internet
Author: User

I. Use of exception handlers

1. Global exception handlers are defined in Filterconfig in ASP.

2. The exception handler, which can be activated in the Web. config file

3. Of course, we can also customize an exception handler, let it inherit Handleerrorattribute, to rewrite the Onexception method

4. The exception handler is defined, then the defined exception handler is registered to the filter

That's it.

Use of two log4net logging components

1. Start by introducing DLL files

2. Writing the configuration file

Configure the Log4net node first in <configuration> under <configSections>

1 <configSections>2 <!--configuration log4net node--3     <section name=" log4net " type="log4net. Config.log4netconfigurationsectionhandler, log4net" />4 </configSections>
View Code

Write a specific configuration

1<log4net>2<!--OFF, FATAL, ERROR, WARN, INFO, DEBUG, all---3<!--Set Root logger level-to-ERROR and its appenders-4<root>5<level value=" All"/>6<appender-ref ref="Sysappender"/>7</root>8<!--Print Only messages of level DEBUG or aboveinchThe Packages--9<logger name="Weblogger">Ten<level value="DEBUG"/> One</logger> A<appender name="Sysappender"Type="log4net. Appender.rollingfileappender,log4net"> <!--output to file-- -<param name="File"Value="app_data/"/> -<param name="Appendtofile"Value="true"/> the<param name="Rollingstyle"Value="Date"/> -<param name="Datepattern"Value="&quot; Logs_&quot;yyyymmdd&quot;. txt&quot;"/> -<param name="Staticlogfilename"Value="false"/> -<layout type="log4net. Layout.patternlayout,log4net"> +<param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n"/> -<param name="Header"Value="& #xD;& #xA,----------------------header--------------------------& #xD;& #xA;"/> +<param name="Footer"Value="& #xD;& #xA,----------------------footer--------------------------& #xD;& #xA;"/> A</layout> at</appender> -<appender name="ConsoleApp"Type="log4net. Appender.consoleappender,log4net"> <!--output to console-- -<layout type="log4net. Layout.patternlayout,log4net"> -<param name="Conversionpattern"Value="%d [%t]%-5p%c-%m%n"/> -</layout> -</appender> in</log4net>
View Code

This code is also looking for others, should be enough.

3. The configuration file is written, it's time to record the captured exception information.

  protected voidApplication_Start () {log4net.  Config.XmlConfigurator.Configure (); //Read Log4net fileArearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (Bundletable.bundles); //Open a thread//Read Exception queue//Exception queue with an exception logged with log4net//no exception thread in exception queue sleep for a whileThreadPool.QueueUserWorkItem (state) = {                //Read exception queue after getting thread                 while(true) {                    if(myexceptionfilter.exqueue.count>0) {Exception ex=MyExceptionFilter.exQueue.Dequeue (); if(Ex! =NULL) {ILog logger= Logmanager.getlogger ("errormsg"); Logger. Error (ex.                        ToString ()); }                        Else{Thread.Sleep ( the); }                    }                      Else{Thread.Sleep ( the);        }                }            }); }
View Code

In the above code

Log4net.                       Config.XmlConfigurator.Configure (); Read Log4net file

ILog logger = Logmanager.getlogger ("errormsg"); ErrorMsg is the name of the log.

Logger. Error (ex.                                                  ToString ()); Record the acquired exception

Get...

Rookie for the first time to write a blog, font these also do not know how much suitable, just the beginning of the code to look at it ...

Catch exceptions in ASP. NET MVC and use the Log4net logging component

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.