• ASP.NET Forums2.x自动捕获异常的基类(补充)


     

         经过一段时间的使用,发现该基类只能自动捕获未处理的异常, 对于用try{}catch (Exception exp){}语句处理过的异常,不能自动捕获。所以,只有手工捕获了:

     

    /// <summary>

             /// 记录捕获的系统异常信息

             /// </summary>

             /// <param name="page"></param>

             /// <param name="exception"></param>

             public static void LogException (Page page,Exception exception)

             {

                  System.Data.SqlClient.SqlException e = (System.Data.SqlClient.SqlException)(exception);

                  string t = "\r\n";

                  string sMessageTmp = "";

                  string sMessage = "";

                  string sSource = "";

                  if (e.Message!=null)

                       sMessageTmp = e.Message.Replace(t,"\\n");

                  if (e.Number > 0)

                       sMessage = String.Format("数据库操作错误!\\n错误号:{0} \\n错误信息:{1}\\n", e.Number, sMessageTmp);

                  if (e.Source!=null)

                       sSource = String.Format("错误来源:{0}", e.Source);

     

                  sSource += page.Request.Url.PathAndQuery;

                  if (e.Procedure!=null)

                       sSource += "    出错对象为:" + e.Procedure;

     

                  using(SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["HexiesoftSqlConnectionString"]))

                  {

                       SqlCommand myCommand = new SqlCommand("ExceptionsInfo_Log", myConnection);

                       myCommand.CommandType = CommandType.StoredProcedure;

     

                       myCommand.Parameters.Add("@Category", SqlDbType.Int).Value = 1;

                       myCommand.Parameters.Add("@Exception", SqlDbType.NVarChar,4000).Value = e.StackTrace.ToString().Trim();

                       myCommand.Parameters.Add("@ExceptionMessage", SqlDbType.NVarChar, 500).Value = sMessage;

                       myCommand.Parameters.Add("@UserAgent", SqlDbType.NVarChar, 64).Value = page.Request.UserAgent;

                       myCommand.Parameters.Add("@IPAddress", SqlDbType.VarChar, 15).Value = page.Request.UserHostAddress;

                       myCommand.Parameters.Add("@HttpReferrer", SqlDbType.NVarChar, 512).Value = page.Request.UrlReferrer.ToString();

                       myCommand.Parameters.Add("@HttpVerb", SqlDbType.NVarChar, 24).Value = page.Request.RequestType;                      ;

                       myCommand.Parameters.Add("@PathAndQuery", SqlDbType.NVarChar, 512).Value = sSource;

                       myConnection.Open();

                       myCommand.ExecuteNonQuery();

                       myConnection.Close();

                  }

             }

     

    调用示例:

                           try

                           {

                                ……

                           }

                           catch (Exception exp)

                           {

                                sqlConnection1.Close(); //更新数据库出错返回之前要关闭sqlConnection1

                                JSUtil.LogException(this,exp);

                                JSUtil.Alert(this,"……失败,请重新再试!"+exp.Message);

                                return;                                       

                           }

  • 相关阅读:
    Python 的编码格式
    Python import其他层级的模块
    自己写ORM框架 DBUtils_DG Java(C#的写在链接里)
    C#对象深度克隆
    SpringMVC文件上传下载
    HttpRuntime.Cache .Net自带的缓存类
    Winform跨窗体操作控件(使用委托)
    Winform调用WebKitBrowser,基于chrome内核WebKit的浏览器控件
    ORM框架 EF code first 的封装 优化一
    Go Language 开发环境搭建
  • 原文地址:https://www.cnblogs.com/ynlxc/p/233152.html
Copyright © 2020-2023  润新知