• .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文件夹下,但是系统异常最好创建一个友好的错误提示页面

  • 相关阅读:
    Java Web的web.xml文件作用及基本配置(转)
    Java Web的传值汇总(含JavaBean)
    路径前面的符号意思(~/,./,../,/)
    MySQL的insert语句的区别
    Linux 安全
    拇指玩」制作的「谷歌安装器」app
    经典角色权限系统设计五张表及拓展应用
    MySQL用户和权限管理
    【基于url权限管理 shiro(一)】--基础
    android图片文件的路径地址与Uri的相互转换
  • 原文地址:https://www.cnblogs.com/niguang/p/5818467.html
Copyright © 2020-2023  润新知