zoukankan      html  css  js  c++  java
  • mvc里全局错误日志

    第一步在项目中找到App_Start文件夹下建立一个错误日志过滤器。

    第二步在Global.asax文件中注册下日志过滤器

    第三步: 继承一个ExceptionFilterAtrribute

    第四步:重写基类

    /// <summary>
    /// 重写基类的异常处理方法
    /// </summary>
    /// <param name="actionExecutedContext"></param>
    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
    //1.异常日志记录(正式项目里面一般是用log4net记录异常日志)
    //HttpContext.Current.Response.Write(
    var LogName = "Log";
    var FileAddress = "Log.txt";
    var path = HttpContext.Current.Server.MapPath("~/");
    string[] temp = path.Split("\".ToCharArray());
    string LevelPath = "";
    for (int i = 0; i < temp.Length - 2; i++)
    {
    LevelPath += temp[i];
    LevelPath += "\";
    }
    if (!Directory.Exists(LevelPath + "\" + LogName))//如果不存在就创建file文件夹
    {
    Directory.CreateDirectory(LevelPath + "\" + LogName);
    }

    FileStream fs = null;
    if (!File.Exists(LevelPath + "\" + LogName + "\" + FileAddress))
    {
    fs = new FileStream(LevelPath + "\" + LogName + "\" + FileAddress, FileMode.Create);
    }
    else
    {
    fs = new FileStream(LevelPath + "\" + LogName + "\" + FileAddress, FileMode.Append);
    }
    StreamWriter sw = new StreamWriter(fs);
    sw.WriteLine("------------------------------------");
    sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    sw.WriteLine("异常信息:" + actionExecutedContext.Exception.GetType().ToString() + "||" + actionExecutedContext.Exception.Message);
    sw.WriteLine("堆栈信息:" + actionExecutedContext.Exception.StackTrace);
    sw.WriteLine("------------------------------------");

    //var d = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "——" +
    // actionExecutedContext.Exception.GetType().ToString() + ":" + actionExecutedContext.Exception.Message + "——堆栈信息:" +
    // actionExecutedContext.Exception.StackTrace;
    ////);

    //2.返回调用方具体的异常信息 501 不支持请求的函数
    if (actionExecutedContext.Exception is NotImplementedException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
    }
    //错误码 408 超时
    else if (actionExecutedContext.Exception is TimeoutException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
    }
    //错误码 403 拒绝访问
    else if (actionExecutedContext.Exception is NotImplementedException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.Forbidden);

    }
    //错误码404
    else if (actionExecutedContext.Exception is NotImplementedException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound);

    }

    //.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
    else
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }

    base.OnException(actionExecutedContext);
    }

    注:  我不知道我描述的清不清楚, 实在看不懂 代码粘贴过去就可以用。

  • 相关阅读:
    jQuery事件
    php学习注意事项
    取消php上传2M的限制(windows服务器)
    PHP编程值得注意的细节
    jQuery load()方法特殊用法!
    PHP显示乱码和apache内部编码问题的解决
    定制Apache索引样式
    这么长时间也没有人看看我
    加载php5apache2_2.dll失败的处理方法
    WinXP下的ApachePHPMySQL安装和配置
  • 原文地址:https://www.cnblogs.com/LoveAndPeace/p/7453655.html
Copyright © 2011-2022 走看看