生产者消费者模式

定义自己的异常过滤器并注册

namespace Eco.Web.App.Models
{
public class MyExceptionAttribute : HandleErrorAttribute
{
public static Queue<Exception> ExceptionQueue = new Queue<Exception>();
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
ExceptionQueue.Enqueue(filterContext.Exception);//将异常信息添加到队列中。
filterContext.HttpContext.Response.Redirect("/Error.html"); }
}
}
namespace Eco.Web.App
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyExceptionAttribute());
}
}
}

开线程写异常信息

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 Spring.Web.Mvc; namespace Spring.Mvc4QuickStart
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801 public class MvcApplication : SpringMvcApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); string fileLogPath = Server.MapPath("/Log/");
//WaitCallback
ThreadPool.QueueUserWorkItem((a) =>
{
while (true)
{
if (MyExceptionAttribute.ExceptionQueue.Count > )
{
Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue();//出队
//string fileName = DateTime.Now.ToString("yyyy-MM-dd")+".txt";
//File.AppendAllText(fileLogPath + fileName, ex.ToString(), System.Text.Encoding.Default);
ILog logger = LogManager.GetLogger("errorMsg");
logger.Error(ex.ToString());
}
else
{
Thread.Sleep();//如果队列中没有数据,休息避免造成CPU的空转.
}
} }, fileLogPath);
} protected override System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver()
{
//get the 'default' resolver, populated from the 'main' config metadata
var resolver = base.BuildWebApiDependencyResolver(); //check if its castable to a SpringWebApiDependencyResolver
var springResolver = resolver as SpringWebApiDependencyResolver; //if it is, add additional config sources as needed
if (springResolver != null)
{
springResolver.AddChildApplicationContextConfigurationLocation("file://~/Config/child_controllers.xml");
} //return the fully-configured resolver
return resolver;
}
}
}

MVC异常日志生产者消费者模式记录(异常过滤器)的更多相关文章

  1. Lucene.net站内搜索—4、搜索引擎第一版技术储备(简单介绍Log4Net、生产者消费者模式)

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

  2. Java 生产者消费者模式详细分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  3. python3全栈开发-多进程的守护进程、进程同步、生产者消费者模式(重点)

    一.守护进程 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic processes a ...

  4. python 并发编程 锁 / 信号量 / 事件 / 队列(进程间通信(IPC)) /生产者消费者模式

    (1)锁:进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的,而共享带来的是竞争,竞争带来的结果就是错乱,如何控制,就是加锁处理. 虽然使用加锁的形式实现了 ...

  5. 【多线程】java多线程实现生产者消费者模式

    思考问题: 1.为什么用wait()+notify()实现生产者消费者模式? wait()方法可以暂停线程,并释放对象锁 notify()方法可以唤醒需要该对象锁的其他线程,并在执行完后续步骤,到了s ...

  6. Celery 框架学习笔记(生产者消费者模式)

    生产者消费者模式 在实际的软件开发过程中,经常会碰到如下场景:某个模块负责产生数据,这些数据由另一个模块来负责处理(此处的模块是广义的,可以是类.函数.线程.进程等).产生数据的模块,就形象地称为生产 ...

  7. 10 阻塞队列 & 生产者-消费者模式

    原文:http://www.cnblogs.com/dolphin0520/p/3932906.html 在前面我们接触的队列都是非阻塞队列,比如PriorityQueue.LinkedList(Li ...

  8. 基于Java 生产者消费者模式(详细分析)

    Java 生产者消费者模式详细分析 本文目录:1.等待.唤醒机制的原理2.Lock和Condition3.单生产者单消费者模式4.使用Lock和Condition实现单生产单消费模式5.多生产多消费模 ...

  9. Java并发(基础知识)—— 阻塞队列和生产者消费者模式

    1.阻塞队列                                                                                        Blocki ...

随机推荐

  1. Number Sequence

    Number Sequence   A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...

  2. 详解CALayer 和 UIView的区别和联系

    详解CALayer 和 UIView的区别和联系   前言 前面发了一篇iOS 面试的文章,在说到 UIView 和 CALayer 的区别和联系的时候,被喵神指出没有切中要点,所以这里就 CALay ...

  3. 李洪强iOS经典面试题128

    1.写一个NSString类的实现 + (id)initWithCString:(c*****t char *)nullTerminatedCString encoding:(NSStringEnco ...

  4. Some useful links

    Integrating the FlyCapture SDK for use with OpenCV CStereoGrabber_Bumblebee.h OpenCV with PGR Flycap ...

  5. SQL server 子查询、设置主键外键、变量及变量查询

    一.子查询 子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这 ...

  6. Layer弹窗组件

    layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会轻松地拥有丰富友好的操作体验. Layer的开发手册和下载地址 http://layer ...

  7. 网页mp3播放代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Maven入门系列(二)--设置中央仓库的方法

    原文地址:http://www.codeweblog.com/maven入门系列-二-设置中央仓库的方法/ Maven仓库放在我的文档里好吗?当然不好,重装一次电脑,意味着一切jar都要重新下载和发布 ...

  9. hdu 4897 Little Devil I

    传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4897 题目大意:一棵树,三个操作:1.将某条链取反,2.将与某条链相邻的边取反,3.查询某条链上为1的边 ...

  10. 【iCore3 双核心板】例程三:EXTI中断输入实验——读取ARM按键状态

    实验指导书及代码包下载: http://pan.baidu.com/s/1o6xToN4 iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...