基本版本 Queue

代码:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Rongzi.BZone.Admin.Functions
{
public class MyExceptionFilterAttribute : System.Web.Mvc.HandleErrorAttribute
{
//版本1:使用预置队列类型存储异常对象
public static Queue<Exception> ExceptionQueue = new Queue<Exception>(); public override void OnException(ExceptionContext filterContext)
{
//将异常信息入队
ExceptionQueue.Enqueue(filterContext.Exception);
//跳转到自定义错误页
filterContext.HttpContext.Response.Redirect("~/Common/CommonError.html"); base.OnException(filterContext);
}
}
}
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Rongzi.BZone.Admin.Functions; namespace Rongzi.BZone.Admin
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute()); // MyExceptionFilterAttribute继承自HandleError,主要作用是将异常信息写入日志文件中
filters.Add(new MyExceptionFilterAttribute());
//filters.Add(new CustomHandleExceptionAttribute(GetError));
} //public static string GetError(System.Exception ex)
//{
// ResponseContext result = new ResponseContext();
// result.Head.Ret = -1;
// result.Head.Code = ErrCode.ParameterError;
// var ret = JsonConvert.SerializeObject(result);
// return ret;
//}
}
}

Global

添加配置

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Net.Http.Formatting;
using Rongzi.BZone.Admin.Functions; namespace Rongzi.BZone.Admin
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
log4net.Config.XmlConfigurator.Configure(
new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "\\log4net.config")); MessageQueueConfig.RegisterExceptionLogQueue();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configuration.EnableCors();
log4net.Config.XmlConfigurator.Configure();
var jsonFormatter = new JsonMediaTypeFormatter();
//GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
GlobalConfiguration.Configuration.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
GlobalConfiguration.Configuration.MessageHandlers.Add(new ResponseDelegatingHandler());
}
}
}

对消息进行处理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Text;
using System.IO; namespace Rongzi.BZone.Admin.Functions
{
public class MessageQueueConfig
{
public static void RegisterExceptionLogQueue()
{
string logFilePath = HttpContext.Current.Server.MapPath("/App_Data/");
//通过线程池开启线程,不停地从队列中获取异常信息并将其写入日志文件
ThreadPool.QueueUserWorkItem(o =>
{
while (true)
{
try
{
if (MyExceptionFilterAttribute.ExceptionQueue.Count > )
{
Exception ex = MyExceptionFilterAttribute.ExceptionQueue.Dequeue(); //从队列中出队,获取异常对象
if (ex != null)
{
//构建完整的日志文件名
string logFileName = logFilePath + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
//获得异常堆栈信息
string exceptionMsg = ex.ToString();
//将异常信息写入日志文件中
File.AppendAllText(logFileName, exceptionMsg, Encoding.Default);
}
}
else
{
Thread.Sleep(); //为避免CPU空转,在队列为空时休息1秒
}
}
catch (Exception ex)
{
MyExceptionFilterAttribute.ExceptionQueue.Enqueue(ex);
}
}
}, logFilePath);
}
}
}

Redis消息队列版本

对上面的进行修改

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Redis;
using System.Configuration;
using ServiceStack.Redis.Generic;
using Rongzi.BZone.Common.Util; namespace Rongzi.BZone.Admin.Functions
{
public class MyExceptionFilterAttribute : System.Web.Mvc.HandleErrorAttribute
{
//版本2:使用Redis的客户端管理器(对象池)
public IRedisClient redisClient = RedisCommon.getInstance.getRedisClient(); public override void OnException(ExceptionContext filterContext)
{
//将异常信息入队
redisClient.EnqueueItemOnList("ExceptionLog", filterContext.Exception.ToString());
//跳转到自定义错误页
filterContext.HttpContext.Response.Redirect("~/Common/CommonError.html"); base.OnException(filterContext);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Text;
using System.IO;
using ServiceStack.Redis;
using System.Configuration;
using ServiceStack.Redis.Generic;
using Rongzi.BZone.Common.Util; namespace Rongzi.BZone.Admin.Functions
{
public class MessageQueueConfig
{
public static IRedisClient redisClient = RedisCommon.getInstance.getRedisClient();
public static void RegisterExceptionLogQueue()
{
//通过线程池开启线程,不停地从队列中获取异常信息并将其写入日志文件
ThreadPool.QueueUserWorkItem(o =>
{
while (true)
{
try
{
if (redisClient.GetListCount("ExceptionLog") > )
{
//从队列中出队,获取异常对象
string errorMsg = redisClient.DequeueItemFromList("ExceptionLog");
if (!string.IsNullOrEmpty(errorMsg))
{
//使用Log4Net写入异常日志
LogHelper.Error(errorMsg);
}
}
else
{
Thread.Sleep(); //为避免CPU空转,在队列为空时休息1秒
}
}
catch (Exception ex)
{
redisClient.EnqueueItemOnList("ExceptionLog", ex.ToString());
}
}
});
}
}
}

http://www.tuicool.com/articles/Ubeyay3

消息队列MQ的更多相关文章

  1. 为什么会需要消息队列(MQ)?

    为什么会需要消息队列(MQ)? #################################################################################### ...

  2. 消息队列一:为什么需要消息队列(MQ)?

    为什么会需要消息队列(MQ)? #################################################################################### ...

  3. 详解RPC远程调用和消息队列MQ的区别

    PC(Remote Procedure Call)远程过程调用,主要解决远程通信间的问题,不需要了解底层网络的通信机制. RPC框架 知名度较高的有Thrift(FB的).dubbo(阿里的). RP ...

  4. 消息队列 MQ 入门理解

    功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...

  5. 消息队列MQ简介

    项目中要用到RabbitMQ,领导让我先了解一下.在之前的公司中,用到过消息队列MQ,阿里的那款RocketMQ,当时公司也做了简单的技术分享,自己也看了一些博客.自己在有道云笔记上,做了一些整理,但 ...

  6. 消息队列MQ集合

    消息队列MQ集合 消息队列简介 kafka简介 Centos7部署zookeeper和Kafka集群 .

  7. 高并发系统:消息队列MQ

    注:前提是知道什么是消息队列.不懂的去搜索各种消息队列入门(activeMQ.rabbitMQ.rocketMQ.kafka) 1.为什么要使用MQ?(MQ的好处:解耦.异步.削峰) (1)解耦:主要 ...

  8. java面试记录三:hashmap、hashtable、concurrentHashmap、ArrayList、linkedList、linkedHashmap、Object类的12个成员方法、消息队列MQ的种类

    口述题 1.HashMap的原理?(数组+单向链表.put.get.size方法) 非线程安全:(1)hash冲突:多线程某一时刻同时操作hashmap并执行put操作时,可能会产两个key的hash ...

  9. 【消息队列MQ】各类MQ比较

    目录(?)[-] RabbitMQ Redis ZeroMQ ActiveMQ JafkaKafka 目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息 ...

  10. 消息队列mq的原理及实现方法

    消息队列技术是分布式应用间交换信息的一种技术.消息队列可驻留在内存或磁盘上,队列存储消息直到它们被应用程序读走.通过消息队列,应用程序可独立地执行--它们不需要知道彼此的位置.或在继续执行前不需要等待 ...

随机推荐

  1. PHP-FPM + Nginx: 502错误

    /etc/php5/fpm/pool.d/www.conf 里面找到这样一段代码: listen = 127.0.0.1:9000 在这上面代码的下面添加一行: listen = /var/run/p ...

  2. PHPStorm下XDebug配置

    PHPStorm下XDebug配置 分类: PHP2013-08-11 22:15 19697人阅读 评论(0) 收藏 举报   目录(?)[+]   1安装Xdebug 用yum安装可能会失败,用p ...

  3. wordpress the_date 方法 偶尔为空的问题

    估计很多人遇到这个问题: 一来是the_title(),the_permalink(),the_date()一路用下来,很正常也很正确 不爱读官方文档,因为文档中有个特别提示 文档:http://co ...

  4. Maximum Likelihood

    https://en.wikipedia.org/wiki/Maximum_likelihood_estimation http://mathworld.wolfram.com/MaximumLike ...

  5. P2661 信息传递 TODO-TARJAN算法

    http://www.cnblogs.com/zbtrs/p/5762788.html http://blog.csdn.net/loi_yzs/article/details/52795093 都是 ...

  6. C++ builder的文件操作

    在编程的过程中,文件的操作是一个经常用到的问题,在C++Builder中,可以使用多种方法对文件操作,下面我就按以下几个部分对此作详细介绍,就是:1.基于C的文件操作:2.基于C++的文件操作:3.基 ...

  7. PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口

    PHP其它常用函数:     赋值:$r->name = "元素";      取值: echo $r->name;  count()   计算数组中的元素数目或对象中 ...

  8. nRF51822之app_button控制uart的开启和关闭

    为什么要使用app_button来控制uart的开启和关闭 还是先上datesheet中uart开启的时候需要HFCLK,需要消耗大量大电流.所以在我们需要的时候需要通过io来通知nrf51822开启 ...

  9. 对于HIVE架构的理解

    1.Hive  能做什么,与 MapReduce 相比优势在哪里 关于hive这个工具,hive学习成本低,入手快,对于熟悉sql语法的人来说,操作简单,熟悉. 2.为什么说 Hive 是 Hadoo ...

  10. Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了

    Qt的学习资料比起其它C/C++的GUI组件来说已经算很全的了.Google的话能解决很多问题,如果没搜到资料的话,如果不是问题太过具体或者奇葩,那就是搜索方法的问题.中文教程中,Qt学习之路系列很不 ...