Distributed Log 2 writes a log with a redis queue

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;usingServicestack.redis;namespaceDistributed Log { Public classMyexceptionfilter:handleerrorattribute {#regionA queue in C #//Public static Queue<exception> listqueue = new queue<exception> (); //Public override void Onexception (Exceptioncontext filtercontext)//{        //if (filtercontext.exception!=null)//    {        //Listqueue.enqueue (filtercontext.exception); //filterContext.HttpContext.Response.Redirect ("/error.html"); //    }        //base.        Onexception (Filtercontext); //}         #endregion        #regionUsing the Redis queue Public StaticIredisclientsmanager clientsmanager=NewPooledredisclientmanager (New string[]{"127.0.0.1:6379"});  Public StaticIredisclient redisclient=clientsmanager.getclient ();  Public Override voidonexception (Exceptioncontext filtercontext) {if(Filtercontext.exception! =NULL) {redisclient.enqueueitemonlist ("Exception", filterContext.Exception.ToString ());//QueueFilterContext.HttpContext.Response.Redirect ("/error.html"); }            Base.        Onexception (Filtercontext); }         #endregion    }}
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading;usingsystem.web;usingSYSTEM.WEB.MVC;usingSystem.Web.Optimization;usingSystem.Web.Routing;namespaceDistributed Log { Public classMvcApplication:System.Web.HttpApplication {protected voidApplication_Start () {arearegistration.registerallareas ();            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (Bundletable.bundles); //open a thread through the thread pool and then read the data from the queue continuously            stringStrroot = Server.MapPath ("/log/"); stringstrpath = strroot + DateTime.Now.ToString ("YYYY-MM-DD"). ToString () +". txt"; ThreadPool.QueueUserWorkItem (i=            {                 while(true)                {                    #regionC # queues//Try//{                    //if (MyExceptionFilter.listQueue.Count > 0)//    {                    //Exception ex = MyExceptionFilter.listQueue.Dequeue (); //if (ex! = null)//        {                    //System.IO.File.AppendAllText (strpath,datetime.now.tostring ("Yyyy-mm-dd HH:mm:ss") + ex.                    ToString () + Environment.newline,system.text.encoding.utf8); //        }                    //Else//        {                    //Thread.Sleep (30); //        }                    //    }                    //Else//    {                    //Thread.Sleep (+);//Avoid CPU idling//    }                    //}                    //catch (Exception ex)//{                    //MyExceptionFilter.listQueue.Enqueue (ex); //}                    #endregion                    #regionRedis queuestringStrkey ="Exception"; Try                    {                        if(MyExceptionFilter.redisClient.GetListCount (strkey) >0)                        {                            stringSTRMSG =MyExceptionFilter.redisClient.DequeueItemFromList (strkey); if(!string. IsNullOrEmpty (STRMSG)) {System.IO.File.AppendAllText (strpath, D AteTime.Now.ToString ("YYYY-MM-DD HH:mm:ss") + STRMSG +Environment.NewLine, System.Text.Encoding.UTF8); }                            Else{Thread.Sleep ( -); }                        }                        Else{Thread.Sleep ( -);//Avoid CPU idling                        }                    }                    Catch(Exception ex) {MyExceptionFilter.redisClient.EnqueueItemOnList (strkey, ex .                    ToString ()); }                    #endregion 
using system.web; using SYSTEM.WEB.MVC; namespace distributed Log {    publicclass  filterconfig    {        public  staticvoid  registerglobalfilters (globalfiltercollection filters)        {            //  Filters. ADD (New Handleerrorattribute ());            Filters. ADD (new  myexceptionfilter ());     }}}

Distributed Log 2 writes a log with a redis queue

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.