1. Custom exception handling filter class files
New MyExceptionAttribute.cs exception Handling class file
The MyExceptionAttribute.cs code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacewebapp.models{ Public classMyexceptionattribute:handleerrorattribute { Public StaticQueue<exception> Exceptionqueue =NewQueue<exception>(); //Exception Handling queue object /// <summary> ///An exception occurred in the Controller method that would call the method to catch the exception/// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonexception (Exceptioncontext filtercontext) {exceptionqueue.enqueue (filtercontext.exception);//writes the captured exception information to the queueFilterContext.HttpContext.Response.Redirect ("/error.html");//Jump to error page Base. Onexception (Filtercontext); } }}
2. Modify the error handling filter to a custom exception handling filter
Modify the following:
usingsystem.web;usingSYSTEM.WEB.MVC;usingWebapp.models;namespacewebapp{ Public classFilterconfig { Public Static voidregisterglobalfilters (globalfiltercollection filters) {//filters. ADD (New Handleerrorattribute ());Filters. ADD (NewMyexceptionattribute ());//filters that use custom exception handling } }}
3. Modify the Global.asax file to open the thread scan exception queue and handle exception information when the program first loads
usingSPRING.WEB.MVC;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingsystem.web;usingSystem.Web.Http;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;usingWebapp.models;namespacewebapp{//Note: For instructions on enabling IIS6 or IIS7 Classic mode,//please visithttp://go.microsoft.com/?LinkId=9394801 Public classMvcapplication:springmvcapplication//System.Web.HttpApplication { protected voidApplication_Start () {arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (Bundletable.bundles); #regionTurn on thread scan for different queues, handling exception informationstringFilelogpath = Server.MapPath ("/log/");//know the folder path used to save the error log file//open a thread scan log queueThreadpool.unsafequeueuserworkitem ((a) = { while(true) { if(MyExceptionAttribute.exceptionQueue.Count >0)//determine if there is data in the queue{Exception ex= MyExceptionAttribute.exceptionQueue.Dequeue ();//out Team if(Ex! =NULL) { stringFileName = DateTime.Now.ToString ("YYYY-MM-DD") +". txt"; File.appendalltext (Filelogpath+ FileName, ex. ToString (), encoding.default);//Write exception append to file } Else{Thread.Sleep ( the); } } Else{Thread.Sleep ( the);//If there is no data in the queue, let the current thread rest for 3 seconds, avoid CPU idling and avoid wasted CPU}}}, Filelogpath); #endregion } }}
4. Test:
Public actionresult Index () { int2; int 0 ; int c = A/ b; return Content (c.tostring ()); }
5. Effects
6. Other Notes:
1. Resolve High concurrency error log issues:
1.1 With the traditional lock method, if the concurrency is very large, the user will be in a situation of stuttering.
1.2. Create a queue in the server, store user error messages, and then open a separate thread to process, there will be no lag situation. (Queue is in memory, writing is fast)
This is the producer-consumer model.
2.Global the Application_Start () method is called only when the program is first executed
3.ThreadPool thread pool threads perform well and are much more efficient than the threads we create ourselves.
4. Pools are used to address frequently created objects, creating consumption performance frequently.
5.Queue is thread-safe and internal multithreading is handled without concurrency problems.
7. Source code Download:
Click to download >>
ASP. NET MVC Custom Exception handling