1. This is the class that handles the exception
Public classMyexceptionattribute:handleerrorattribute {//Public static Queue<exception> exceptionqueue = new queue<exception> (); //the project uses the following method to create the Redisclient Public StaticIredisclientsmanager Clientmanager =NewPooledredisclientmanager (New string[] {"127.0.0.1:6379" }); Public StaticIredisclient redisclient =clientmanager.getclient (); /// <summary> ///Catching exceptions/// </summary> /// <param name= "Filtercontext" ></param> Public Override voidonexception (Exceptioncontext filtercontext) {Base. Onexception (Filtercontext); Exception ex=filtercontext.exception; //Write to queue//Exceptionqueue.enqueue (ex);Redisclient.enqueueitemonlist ("Errorqueue", ex. ToString ()); //jump to the error page.FilterContext.HttpContext.Response.Redirect ("/error.html"); } }
Configuration in 2.Global files
Public classMvcapplication:springmvcapplication//System.Web.HttpApplication { protected voidApplication_Start () {log4net. Config.XmlConfigurator.Configure ();//Read about log4net configuration information in the configuration file.Arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); //opens a thread that scans the exception message queue. stringFilePath = Server.MapPath ("/log/"); ThreadPool.QueueUserWorkItem ((a)= { while(true) { //determine if there is data in the queue//if (MyExceptionAttribute.ExceptionQueue.Count () > 0) if(Myexceptionattribute.redisclient. GetListCount ("Errorqueue") >0) { stringErrorMsg =MyExceptionAttribute.redisClient.DequeueItemFromList ("Errorqueue"); { //Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue (); if(!string. IsNullOrEmpty (errormsg)) {//writes exception information to the log file. //string fileName = DateTime.Now.ToString ("Yyyy-mm-dd"); //File.appendalltext (filepath+filename+ ". txt", ex. ToString (), System.Text.Encoding.UTF8);ILog logger = Logmanager.getlogger ("errormsg"); Logger. Error (ERRORMSG); } Else { //If there is no data in the queue, restThread.Sleep ( the); } } } Else { //If there is no data in the queue, restThread.Sleep ( the); }}},filepath); } }
3. Don't forget to use the 3 DLL files required by Redis
Using Redis distributed queues