标签:
1.这是处理异常的类
public class MyExceptionAttribute:HandleErrorAttribute { //public static Queue<Exception> ExceptionQueue = new Queue<Exception>(); //项目中使用下面方式创建redisclient public static IRedisClientsManager clientManager = new PooledRedisClientManager(new string[] { "127.0.0.1:6379" }); public static IRedisClient redisClient = clientManager.GetClient(); /// <summary> /// 捕获异常 /// </summary> /// <param name="filterContext"></param> public override void OnException(ExceptionContext filterContext) { base.OnException(filterContext); Exception ex = filterContext.Exception; //写到队列 //ExceptionQueue.Enqueue(ex); redisClient.EnqueueItemOnList("errorQueue", ex.ToString()); //跳转到错误页面. filterContext.HttpContext.Response.Redirect("/Error.html"); } }
2.Global文件中配置
public class MvcApplication : SpringMvcApplication //System.Web.HttpApplication { protected void Application_Start() { log4net.Config.XmlConfigurator.Configure();//读取了配置文件中关于Log4Net配置信息. AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); //开启一个线程,扫描异常信息队列。 string filePath = Server.MapPath("/Log/"); ThreadPool.QueueUserWorkItem((a) => { while (true) { //判断一下队列中是否有数据 //if (MyExceptionAttribute.ExceptionQueue.Count() > 0) if (MyExceptionAttribute.redisClient.GetListCount("errorQueue") > 0) { string errorMsg = MyExceptionAttribute.redisClient.DequeueItemFromList("errorQueue"); { //Exception ex = MyExceptionAttribute.ExceptionQueue.Dequeue(); if (!string.IsNullOrEmpty(errorMsg)) { //将异常信息写到日志文件中。 //string fileName = DateTime.Now.ToString("yyyy-MM-dd"); //File.AppendAllText(filePath+fileName+".txt",ex.ToString(),System.Text.Encoding.UTF8); ILog logger = LogManager.GetLogger("errorMsg"); logger.Error(errorMsg); } else { //如果队列中没有数据,休息 Thread.Sleep(3000); } } } else { //如果队列中没有数据,休息 Thread.Sleep(3000); } } },filePath); } }
3.不要忘记使用Redis需要的3个dll文件
标签:
原文地址:http://www.cnblogs.com/xtxtx/p/5350266.html