Using Redis distributed queues

Source: Internet
Author: User
Tags log4net

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

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.