zoukankan      html  css  js  c++  java
  • .net MVC中异常日志

        在日常工作中,我们有些项目可能进入了维护期,但是项目可能存在一些潜伏较深的bug导致我们在测试阶段并未发现,那么错误日志记录为我们的项目维护起着重要的作用。记录系统日志的方法如下

       1.在系统根目录建立Log文件夹

       2.创建异常类,且该类继承FilterAttribute, IExceptionFilter

    public class LogExceptionFilterAttribute : FilterAttribute, IExceptionFilter
    {
          private string ErrPath = System.Web.HttpContext.Current.Server.MapPath("/Log");
      /// <summary>
      /// 重写异常方法
      /// </summary>
      /// <param name="filterContext"></param>
      public void OnException(ExceptionContext filterContext)
      {
        if (!Directory.Exists(ErrPath))
        {
          Directory.CreateDirectory(ErrPath);
        }
        File.AppendAllText(ErrPath + "/" + DateTime.Now.Date.ToString("yyyy-MM-dd") + ".txt", filterContext.Exception.Message + " 对象:" + filterContext.Exception.Source + " 方法:" + filterContext.Exception.TargetSite + " 字符串:" + filterContext.Exception.StackTrace + " 时间:" + DateTime.Now + " " + HttpContext.Current.Request.Url.PathAndQuery + " ---------------------------------- ");
      }
    }

      3.注册全局过滤器

      找到App_start文件夹下的filterconfig类,并注册异常过滤器

    public class FilterConfig
    {
      public static void RegisterGlobalFilters(GlobalFilterCollection filters)
      {
        filters.Add(new LogExceptionFilterAttribute());
        filters.Add(new HandleErrorAttribute());
      }
    }

    4.此时若系统中有异常出现,在该异常信息会被记录在Log文件夹下,但是系统异常最好创建一个友好的错误提示页面

  • 相关阅读:
    让程序用自定义的菜单自定义菜单AVKON_VIEW,CBA,MENU_BAR,MENU_PANE
    symbian 菜单不显示的原因
    子类中调用父类的带参数的构造函数|子类构造函数调用父类构造函数 的说明
    symbian 设置 透明背景
    IOS App资源路径
    Nonblock I/O 及其使用
    CEikStatusPane MakeVisible kernexec 3错误
    把mapinfo图层的经纬度信息导出来的办法
    解决安装macports,不能更新的问题
    jpg结构解析
  • 原文地址:https://www.cnblogs.com/niguang/p/5818467.html
Copyright © 2011-2022 走看看