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
                    {
                        )
                        {
                            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();
                            }
                        }
                        else
                        {
                            Thread.Sleep();//避免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的队列写日志的更多相关文章

  1. 分布式日志1 用c#的队列写日志

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. 重复造轮子,编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)

    一说到写日志,大家可能推荐一堆的开源日志框架,如:Log4Net.NLog,这些日志框架确实也不错,比较强大也比较灵活,但也正因为又强大又灵活,导致我们使用他们时需要引用一些DLL,同时还要学习各种用 ...

  3. python 14篇 写日志

    import sys from loguru import logger # 日志级别: debug 调试信息打印日志比较详细,级别最低 # info 正常的提示信息,级别较低 # waring 警告 ...

  4. Redis 自定义 RedisAppender 插件, 实现日志缓冲队列,集中日志输出.

    因为某些异步日志设置了即使队列满了,也不可丢弃,在并发高的时候,导致请求方法同步执行,响应变慢. 编写这个玩意,除了集中日志输出以外,还希望在高并发的时间点有缓冲作用. 之前用Kafka实现了一次入队 ...

  5. 利用StackExchange.Redis和Log4Net构建日志队列

    简介:本文是一个简单的demo用于展示利用StackExchange.Redis和Log4Net构建日志队列,为高并发日志处理提供一些思路. 0.先下载安装Redis服务,然后再服务列表里启动服务(R ...

  6. java-spring基于redis单机版(redisTemplate)实现的分布式锁+redis消息队列,可用于秒杀,定时器,高并发,抢购

    此教程不涉及整合spring整合redis,可另行查阅资料教程. 代码: RedisLock package com.cashloan.analytics.utils; import org.slf4 ...

  7. c# 多线程使用队列顺序写日志的类 (需要再优化)

    using System; using System.Collections.Generic; using System.Threading; public class LogManager { // ...

  8. Docker部署Django项目+Nginx+Fluend日志收集 和redis、memcached、RabbitMQ、Celery

    前言 一.docker 1.docker是什么? Docker的英文本意是“搬运工”,Docker搬运的是集装箱(Container)可以成为容器,我可以把写的Django的WEB应用以及Python ...

  9. ELK(+Redis)-开源实时日志分析平台

    ################################################################################################### ...

随机推荐

  1. Yii里表单的操作方法(展示渲染待续......)

    <?php$form=\yii\widgets\ActiveForm::begin(['action'=>\yii\helpers\Url::to('name/create')]);ech ...

  2. gc之四--Minor GC、Major GC和Full GC之间的区别

    针对HotSpot VM的实现,它里面的GC其实准确分类只有两大种: Partial GC:并不收集整个GC堆的模式 Young GC:只收集young gen的GC Old GC:只收集old ge ...

  3. gerrit集成gitweb:Error injecting constructor, java.io.IOException: Permission denied

    使用gerrit账户在centos上安装gerrit,然后集成gitweb,gerrit服务启动失败,查看日志,报错信息如下: [-- ::,] ERROR com.google.gerrit.pgm ...

  4. 每天一个 Linux 命令(13):less 命令

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性. 在 more 的时候,我们并没有办法向前面 ...

  5. 在脚本中刷新impala元信息

    刷新impala元信息 impala-shell -q 'invalidate metadata' -i hslave1 impala-shell -q 'select count(*) from p ...

  6. XMPP协议错误码

    302 重定向 尽管HTTP规定中包含八种不同代码来表示重定向,Jabber只用了其中一个(用来代替所有的重定向错误).不过Jabber代码302是为以后的功能预留的,目前还没有用到 400 坏请求  ...

  7. 课堂笔记--Strom并发模型

    Strom并发模型:     topology是如何运行的?(可与mapreduce对比)         第一层:cluster         第二层:supervisor(host.node.机 ...

  8. click事件

    click事件是可以多次绑定的,如果绑定多次就会执行多次,因此再不需要重复执行的情况下,就需要使用unbind对事件进行解绑

  9. set JAVA_HOME in RHEL/CentOS

    3.3. Install OpenJDK on Red Hat Enterprise Linux Introduction OpenJDK is one of many Java Developmen ...

  10. js调用java代码返回解决方案

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 今天封装一个加密标签,遇到一个问题,我需要对页面上的数据调用java后台代码进行解密,而标签里只能通过js获取到数据,所以就遇 ...