zoukankan      html  css  js  c++  java
  • 普通错误日志记录。

    在开发项目的过程中,我们会遇到各种各样的错误,那么接下来就说一下普通记录错误日志的功能。

    1、先导入命名

    1、MVC中有一个异常过滤器,自己建立一个过滤器,并继承自HandleErrorAttribute,在重写一下OnException()方法。  

     1 namespace OA.APP.Error
     2 {
     3     public class MyExceptionAttribute:HandleErrorAttribute
     4     {
     5         //写一个队列,队列类型是Exception类型,只要抛异常了,都同写一个队列,因此是静态的。
     6         //Queue:是一个队列类型。
     7         public 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() + "----------->异常信息如下:
    ";
    17         string endExceptionStr = "
    
    ";
    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、如果请求的控制器抛异常了,那么会显示错误信息。

        

    5、格式如下:

      

    End。

  • 相关阅读:
    数据库DQL(Data Query Language)语言学习之三:排序查询
    数据库DQL(Data Query Language)语言学习之二:条件查询
    MySQL中的通配符
    SQL查询文档网站
    python之特殊方法
    java之静态函数和静态变量
    java之类和对象
    python之类的继承
    python的面向对象编程
    python之模块(在命令行当中使用pip install 模块来进行模块的安装)
  • 原文地址:https://www.cnblogs.com/xiezunxu/p/7865882.html
Copyright © 2011-2022 走看看