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