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

  • 相关阅读:
    调整精力记录
    cucumber系列(四) RubyGems下载源更新的问题
    cucumber系列(三)BDD与相关测试框架资料收集
    cucumber系列(二) cucumber的基本操作命令
    cucumber系列(一) 如何让cucumber识别中文
    centos与ubuntu的区别 (转)
    Excel 导出组件,10W级数据5秒内导出
    如何本地调试测试环境的代码
    .net core 在Startup.cs 的Configure方法中扩展 IApplicationBuilder
    mysql 统计一周每天得数据
  • 原文地址:https://www.cnblogs.com/niguang/p/5818467.html
Copyright © 2011-2022 走看看