zoukankan      html  css  js  c++  java
  • 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;                                       

                           }

  • 相关阅读:
    Java 代码块
    Java 方法签名
    Java 中的继承
    Java 中的this关键字
    Java 静态对象 static
    Java报错 -- The public type c must be defined in its own file
    Java 构造方法
    Java 成员变量和局部变量
    Java 对象的创建和使用
    JavaScript单线程和异步机制
  • 原文地址:https://www.cnblogs.com/ynlxc/p/233152.html
Copyright © 2011-2022 走看看