基本版本 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