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;                                       

                           }

  • 相关阅读:
    window.open的火狐、谷歌兼容写法
    一个分数怎样约分?想知道的速度了。。。
    这是第二道题内容要求写一个银行的ATM系统 这个浪费了好长时间 ,遇到了许多问题,不过都解决了,上程序
    两个有理数相加(要求输入时以分数形式,输出时也以分数形式)
    linux centos 7.5下 源码编译安装 lua环境
    SecureCRT 6.7 vim高亮
    C#第一章笔记
    HTML5考试错题
    第九章博客
    第八章博客
  • 原文地址:https://www.cnblogs.com/ynlxc/p/233152.html
Copyright © 2011-2022 走看看