普通错误日志记录。
2017-11-20 13:39
148 查看
在开发项目的过程中,我们会遇到各种各样的错误,那么接下来就说一下普通记录错误日志的功能。
1、先导入命名
1、MVC中有一个异常过滤器,自己建立一个过滤器,并继承自HandleErrorAttribute,在重写一下OnException()方法。
2、在App_Start()文件夹中的FilterConfig类中注册自己写的过滤器。
3、在程序入口的地方(也就是Global文件中)开一个线程,用于扫描异常,并让Global文件继承自SpringMvcApplication。
4、如果请求的控制器抛异常了,那么会显示错误信息。
![](https://images2017.cnblogs.com/blog/911026/201711/911026-20171120133810805-1390116561.png)
5、格式如下:
![](https://images2017.cnblogs.com/blog/911026/201711/911026-20171120133904493-465381131.png)
End。
1、先导入命名
1、MVC中有一个异常过滤器,自己建立一个过滤器,并继承自HandleErrorAttribute,在重写一下OnException()方法。
1 namespace OA.APP.Error 2 { 3 public class MyExceptionAttribute:HandleErrorAttribute 4 { 5 //写一个队列,队列类型是Exception类型,只要抛异常了,都同写一个队列,因此是静态的。 6 //Queue:是一个队列类型。 7 blic Queue<Exception> queueException = new Queue<Exception>(); 8 9 /// <summary> 10 /// 重写OnException方法,用于捕获异常数据。 11 /// </summary> 12 /// <param name="filterContext"></param> 13 public override void OnException(ExceptionContext filterContext) 14 { 15 base.OnException(filterContext); 16 //获取异常对象。 17 Exception ex = filterContext.Exception; 18 //把错误写入到队列中。 19 queueException.Enqueue(ex); 20 //跳转到自己定义的错误页。 21 filterContext.HttpContext.Response.Redirect("/Error/ErrorHtml.html"); 22 } 23 } 24 }
2、在App_Start()文件夹中的FilterConfig类中注册自己写的过滤器。
1 public class FilterConfig 2 { 3 public static void RegisterGlobalFilters(GlobalFilterCollection filters) 4 { 5 //filters.Add(new HandleErrorAttribute()); 6 //注册自己写的异常过滤器。 7 filters.Add(new Error.MyExceptionAttribute()); 8 } 9 }
3、在程序入口的地方(也就是Global文件中)开一个线程,用于扫描异常,并让Global文件继承自SpringMvcApplication。
1 //public class MvcApplication : System.Web.HttpApplication 2 //SpringMvcApplication 继承自System.Web.HttpApplication。 3 public class MvcApplication : SpringMvcApplication 4 { 5 protected void Application_Start() 6 { 7 log4net.Config.XmlConfigurator.Configure(); 8 9 AreaRegistration.RegisterAllAreas(); 10 WebApiConfig.Register(GlobalConfiguration.Configuration); 11 FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 12 RouteConfig.RegisterRoutes(RouteTable.Routes); 13 BundleConfig.RegisterBundles(BundleTable.Bundles); 14 15 string fullPath = Server.MapPath("/Error/ErrorLog.txt"); 16 string beginExceptionStr = DateTime.Now.ToString() + "----------->异常信息如下:\r\n"; 17 string endExceptionStr = "\r\n\r\n"; 18 19 //QueueUserWorkItem()将方法排入列队以便执行。在匿名函数中判断队列中是否有数据,以及写错误信息到日志中。 20 //开启一个线程,扫描异常信息队列。 21 ThreadPool.QueueUserWorkItem((c) => 22 { 23 while (true) 24 { 25 //判断队列中是否否有数据。 26 if (MyExceptionAttribute.QueueException.Count > 0) 27 { 28 //通过Dequeue()方法,拿到异常对象。 29 Exception ex = MyExceptionAttribute.QueueException.Dequeue(); 30 if (ex != null) 31 { 32 //将异常对象,写入到日志中。 33 //使用File类的时候,如果文件不存在就会自己创建。 34 //如果请求的控制器出现异常了,会在Error文件夹中生成一个ErrorLog.txt文件。 35 File.AppendAllText(fullPath, beginExceptionStr + ex.ToString() + endExceptionStr); 36 } 37 else 38 { 39 System.Threading.Thread.Sleep(3000); 40 } 41 } 42 else 43 { 44 //休眠3秒。 45 Thread.Sleep(3000); 46 } 47 } 48 }); 49 } 50 }
4、如果请求的控制器抛异常了,那么会显示错误信息。
![](https://images2017.cnblogs.com/blog/911026/201711/911026-20171120133810805-1390116561.png)
5、格式如下:
![](https://images2017.cnblogs.com/blog/911026/201711/911026-20171120133904493-465381131.png)
End。
相关文章推荐
- YII2 日志模块 之 使用数据库记录错误信息
- txt文件记录错误日志
- php.ini配置--记录PHP错误日志
- 错误日志记录和邮件提醒的功能
- asp.net Web项目中使用Log4Net进行错误日志记录
- spring aop记录操作日志和错误日志
- EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因
- 使用自治事务记录日志的错误信息
- 实例学习SSIS(四)--使用日志记录和错误流重定向
- 使用Global.asax在ASP.NET中记录错误日志
- Java Web Project自定义错误页面,log4j记录日志。
- 网站记录错误日志的工具ELMAH
- Log4net 框架系列:log4net日志文件在asp.net中的应用实例-记录系统错误
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- C# 记录错误日志
- asp.net mvc中加入log4net记录错误日志
- CodeIgniter记录错误日志的方法全面总结
- MOSS中图形化通过记录日志的方式来进行查看“发生意外错误”的原因WSP下载
- 错误日志记录
- 点滴积累【C#】---错误日志记录到txt文本里。