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. [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)

    写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...

  2. EF 示例

    EF有三种数据库访问方式,这里只介绍Code First. 1.DB First,类似Linq to sql中拖拽一个DB到方案中 2.Model First,没试过,不能自动生成数据库只能生成表 3 ...

  3. SQL Server2008创建约束图解

    机房收费系统个人重构版开始了,但在设计数据库时遇到了写小麻烦,主要是数据库中约束的应用,以前在学习数据库时进行了总结,在刚开始学习时使用的是SQL Server2000 小操作了下,查证了几种约束的作 ...

  4. google快捷键,通过浏览器本身来查看

    今天玩google浏览器时发现一个不需要访问google就能查看其快捷键的方式,再此记录一下,以备后用. 1:打开Google浏览器 2:打开开发者工具,有三种方法 2-1:按F12快捷键 2-2:按 ...

  5. HTTP的应用httpclient 和线程

    package com.example.service; import java.io.IOException; import java.io.InputStream; import org.apac ...

  6. Explode TArray

    function Explode(const Separator, S: string; Limit: Integer = 0): TArray;var SepLen : Integer; F, P ...

  7. 关于sql注入

    删除表,先猜表名,可以使用下面的语名: Select * from A where A.a = ‘testdata’; drop table A---’; If a field only allow ...

  8. codeforce div 377

    #include <bits/stdc++.h> using namespace std; #define pb push_back #define lb lower_bound #def ...

  9. 【C#】 一些不常用,很容易混淆的知识点

    [C#] 一些不常用但很容易混淆的知识点 1. 访问修饰符 internal ,译为内部的, 在同一个程序集中可访问,它的内部是相对与程序集的,可不能想当然了 2. String.Compare 这个 ...

  10. EF联合查询的新用法

    用EF很多年了,做联合查询时,只知道linq和lambda两种语法,今天朋友发了一个链接,打开看后发现是EF内置的新的关于联合查询的方法,赶紧抄录下来,以备后用. 现在先把这几种方法,各写一个例子,便 ...