分布式日誌2 用redis的隊列寫日誌

來源:互聯網
上載者:User

標籤:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using ServiceStack.Redis;namespace 分布式日誌{    public class MyExceptionFilter : HandleErrorAttribute    {        #region 用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        #region 用redis的隊列        public static IRedisClientsManager clientsManager=new PooledRedisClientManager(new string[]{"127.0.0.1:6379"});        public static IRedisClient redisClient=clientsManager.GetClient();        public override void OnException(ExceptionContext filterContext)        {            if (filterContext.Exception != null)            {                redisClient.EnqueueItemOnList("exception",filterContext.Exception.ToString());//入隊                filterContext.HttpContext.Response.Redirect("/error.html");            }            base.OnException(filterContext);        }         #endregion    }}
using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Web;using System.Web.Mvc;using System.Web.Optimization;using System.Web.Routing;namespace 分布式日誌{    public class MvcApplication : System.Web.HttpApplication    {        protected void Application_Start()        {            AreaRegistration.RegisterAllAreas();            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);            BundleConfig.RegisterBundles(BundleTable.Bundles);            //通過線程池開啟一個線程,然後不停的從隊列中讀取資料            string strRoot = Server.MapPath("/Log/");            string strPath = strRoot + DateTime.Now.ToString("yyyy-MM-dd").ToString()+".txt";            ThreadPool.QueueUserWorkItem(i =>            {                while (true)                {                    #region c# 隊列                    //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(30);//避免cpu空轉                    //    }                    //}                    //catch(Exception ex)                    //{                    //    MyExceptionFilter.listQueue.Enqueue(ex);                    //}                    #endregion                    #region redis 隊列                    string strKey = "exception";                    try                    {                        if (MyExceptionFilter.redisClient.GetListCount(strKey) > 0)                        {                            string strMsg = MyExceptionFilter.redisClient.DequeueItemFromList(strKey);                            if (!string.IsNullOrEmpty(strMsg))                            {                                System.IO.File.AppendAllText(strPath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + strMsg + Environment.NewLine, System.Text.Encoding.UTF8);                            }                            else                            {                                Thread.Sleep(30);                            }                        }                        else                        {                            Thread.Sleep(30);//避免cpu空轉                        }                    }                    catch (Exception ex)                    {                        MyExceptionFilter.redisClient.EnqueueItemOnList(strKey, ex.ToString());                    }                    #endregion                 }            }, strPath);        }    }}
using System.Web;using System.Web.Mvc;namespace 分布式日誌{    public class FilterConfig    {        public static void RegisterGlobalFilters(GlobalFilterCollection filters)        {            //filters.Add(new HandleErrorAttribute());            filters.Add(new MyExceptionFilter());        }    }}

 

分布式日誌2 用redis的隊列寫日誌

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.