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 { if (MyExceptionFilter.redisClient.GetListCount(strKey) > 0) { 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(30); } } else { Thread.Sleep(30);//避免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()); } } }
时间: 2024-10-12 19:24:58