经过一段时间的使用,发现该基类只能自动捕获未处理的异常, 对于用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;
}