码迷,mamicode.com
首页 > 其他好文 > 详细

分布式日志2 用redis的队列写日志

时间:2016-09-16 11:20:30      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

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的队列写日志

标签:

原文地址:http://www.cnblogs.com/caohuimingfa/p/5876042.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!