欢迎来到天天文库
浏览记录
ID:21972387
大小:151.98 KB
页数:6页
时间:2018-10-26
《关于面向切面编程的部分内容-错误处理机制》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、关于而向切而编程的部分內容•错误处理机制错误处理机制。面对多个web服务器,多线程处理,我们想把错误信息记录到一个txt文档中。但是把错误信息写到內存是很快。写到硬盘上就有一堆的问题。比如说读写慢、并发问题。今天我们就利用这个实现错误处理此文以MVC为例1、首先要在go1able文件的protectedvoidApplication_Start()注册一个错误处理机制。MVC中自带一个过滤器FilterConfig.RegisterGlobalFiIters(GlobalFilters.Filters);2、文件夹下面的FilterConfig.cs文件'App^Start>BundleConfig.es>iC.FilterConfig.es>sc®RouteConfig.es>WebApiConfig.es>BriConfig3、打开FilterConfig.es文件就写了一个注册事件。我们看得到这个是对错误处理机制filter*./3、blicclassMyExceptionAttribute:HandleErrorAttribute2.{3.//privatestaticobjectobj=newobject();4.publicstaticConcurrentQueueExceptionQueue=newConcurrentQueue10.publicoverridev4、oidOnException(ExceptionContextfilterContext)11.{12.13-base.OnException(filterContext);14.Exceptionex=filterContext•Exception;//捕获异常信息。15.//将异常信息写到队列中。16.ExceptionQueue.Enqueue(ex);17.//跳转到错误页而.18.filterContext.HttpContext.Response.Redirect("/Error.html");19.20.21.22.}23.}24.25.主要是定义一个静态队列C5、oncurrentQueue(当然你也可以用Queue。但是微软说这个ConcurrentQueue比Queue安全。好像是线程安全的,一堆堆的理论,说白了就是用ConcurrentQueue更安全)这样所有的错误就都在这个队列里面了。(就是内存)这样总不行吧。内存断电就没有了的啊。所以我们要想把资料存到硬盘屮。5、现在又要在golable文件的protectedvoidApplicationStart()中注册一个消费线程(这句话后面会解释,看不懂就继续)就是在protectedvoidApplication_Start()中加入这些代码,最好放最前而。内容就是线程池升启一6、个线程从刚刚定义的MyExceptionAttribute的ExceptionQueue队列里面取出项来。将错误信息最加到文件后面。如果队列为空,就线程停留3秒。1.stringfilePath=Server.MapPath('*/Log/");2.ThreadPool.QueueUserWorkltem((a)=>3.{4.5.while(true)//注意:线程不能结朿。后面写到队列屮的数据没法处理。6.{7.8.//这里可以加—条if(MyExceptionAttribute.ExceptionQueue.CountO>0)9.//{发送邮件到管理员}10if(MyEx7、ceptionAttribute.ExceptionQueue.Count()>0)11.{12.//Exceptionex=MyExceptionAttribute.ExceptionQueue.Dequeue();//从队列中取出数据.13.Exceptionex=null;14.boolisResult=MyExceptionAttribute.ExceptionQueue.TryDequeue(outex);15.if(ex!=null&&isResult)16.{17.stringfullPa
2、文件夹下面的FilterConfig.cs文件'App^Start>BundleConfig.es>iC.FilterConfig.es>sc®RouteConfig.es>WebApiConfig.es>BriConfig3、打开FilterConfig.es文件就写了一个注册事件。我们看得到这个是对错误处理机制filter*./3、blicclassMyExceptionAttribute:HandleErrorAttribute2.{3.//privatestaticobjectobj=newobject();4.publicstaticConcurrentQueueExceptionQueue=newConcurrentQueue10.publicoverridev4、oidOnException(ExceptionContextfilterContext)11.{12.13-base.OnException(filterContext);14.Exceptionex=filterContext•Exception;//捕获异常信息。15.//将异常信息写到队列中。16.ExceptionQueue.Enqueue(ex);17.//跳转到错误页而.18.filterContext.HttpContext.Response.Redirect("/Error.html");19.20.21.22.}23.}24.25.主要是定义一个静态队列C5、oncurrentQueue(当然你也可以用Queue。但是微软说这个ConcurrentQueue比Queue安全。好像是线程安全的,一堆堆的理论,说白了就是用ConcurrentQueue更安全)这样所有的错误就都在这个队列里面了。(就是内存)这样总不行吧。内存断电就没有了的啊。所以我们要想把资料存到硬盘屮。5、现在又要在golable文件的protectedvoidApplicationStart()中注册一个消费线程(这句话后面会解释,看不懂就继续)就是在protectedvoidApplication_Start()中加入这些代码,最好放最前而。内容就是线程池升启一6、个线程从刚刚定义的MyExceptionAttribute的ExceptionQueue队列里面取出项来。将错误信息最加到文件后面。如果队列为空,就线程停留3秒。1.stringfilePath=Server.MapPath('*/Log/");2.ThreadPool.QueueUserWorkltem((a)=>3.{4.5.while(true)//注意:线程不能结朿。后面写到队列屮的数据没法处理。6.{7.8.//这里可以加—条if(MyExceptionAttribute.ExceptionQueue.CountO>0)9.//{发送邮件到管理员}10if(MyEx7、ceptionAttribute.ExceptionQueue.Count()>0)11.{12.//Exceptionex=MyExceptionAttribute.ExceptionQueue.Dequeue();//从队列中取出数据.13.Exceptionex=null;14.boolisResult=MyExceptionAttribute.ExceptionQueue.TryDequeue(outex);15.if(ex!=null&&isResult)16.{17.stringfullPa
3、blicclassMyExceptionAttribute:HandleErrorAttribute2.{3.//privatestaticobjectobj=newobject();4.publicstaticConcurrentQueueExceptionQueue=newConcurrentQueue10.publicoverridev
4、oidOnException(ExceptionContextfilterContext)11.{12.13-base.OnException(filterContext);14.Exceptionex=filterContext•Exception;//捕获异常信息。15.//将异常信息写到队列中。16.ExceptionQueue.Enqueue(ex);17.//跳转到错误页而.18.filterContext.HttpContext.Response.Redirect("/Error.html");19.20.21.22.}23.}24.25.主要是定义一个静态队列C
5、oncurrentQueue(当然你也可以用Queue。但是微软说这个ConcurrentQueue比Queue安全。好像是线程安全的,一堆堆的理论,说白了就是用ConcurrentQueue更安全)这样所有的错误就都在这个队列里面了。(就是内存)这样总不行吧。内存断电就没有了的啊。所以我们要想把资料存到硬盘屮。5、现在又要在golable文件的protectedvoidApplicationStart()中注册一个消费线程(这句话后面会解释,看不懂就继续)就是在protectedvoidApplication_Start()中加入这些代码,最好放最前而。内容就是线程池升启一
6、个线程从刚刚定义的MyExceptionAttribute的ExceptionQueue队列里面取出项来。将错误信息最加到文件后面。如果队列为空,就线程停留3秒。1.stringfilePath=Server.MapPath('*/Log/");2.ThreadPool.QueueUserWorkltem((a)=>3.{4.5.while(true)//注意:线程不能结朿。后面写到队列屮的数据没法处理。6.{7.8.//这里可以加—条if(MyExceptionAttribute.ExceptionQueue.CountO>0)9.//{发送邮件到管理员}10if(MyEx
7、ceptionAttribute.ExceptionQueue.Count()>0)11.{12.//Exceptionex=MyExceptionAttribute.ExceptionQueue.Dequeue();//从队列中取出数据.13.Exceptionex=null;14.boolisResult=MyExceptionAttribute.ExceptionQueue.TryDequeue(outex);15.if(ex!=null&&isResult)16.{17.stringfullPa
此文档下载收益归作者所有