.NET - 使用多个 SqlParameters 将异常记录到数据库并指定每个数据库列中的内容
.NET - log exceptions to db using multiple SqlParameters and specify what goes in each db column
我正在按照 KudVenkat
的教程将异常记录到 .NET
中的数据库:http://csharp-video-tutorials.blogspot.com/2012/12/logging-exception-to-database-part-75.html
他的方法很适合存储一件事——一个包含异常消息、异常类型、堆栈跟踪等的 ExceptionMessage 字符串——到一列——@exceptionMessage
列。
我试图将这个完整的异常消息分成几个部分,例如 ExceptionType(到 @exceptionType 列)、exceptionMessage(到 @exceptionMessage 列)、StackTrace(到 @stackTrace 列)等。
到目前为止,我可以使用以下方法传入多个 SqlParameters
:
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", log) { },
new SqlParameter("@exceptionMessage", log) { }
};
cmd.Parameters.AddRange(sp.ToArray());
但我似乎无法将这些数据拆分成我想要的列 - 我这样做的方式是,我必须为每个 SqlParameters 调用我的记录器函数(请参阅我的下面的代码注释),它们各自创建一个新行,每一列上都有相同的消息。它不是 Type
转到类型列,Message
转到消息列,而是创建一行 Message
插入类型和消息列,然后是另一行 Type
插入类型和消息列。
我认为这与我的 LogToDB 的性质有关 class:
public class storedProcedure : ApiController
{
public static void Log(Exception exception)
{
// exception type
StringBuilder sbExceptionType = new StringBuilder();
// exception message
StringBuilder sbExceptionMessage = new StringBuilder();
do
{
// exception type
sbExceptionType.Append("Exception Type" + Environment.NewLine);
sbExceptionType.Append(exception.GetType().Name);
sbExceptionType.Append(Environment.NewLine + Environment.NewLine);
// exception message
sbExceptionMessage.Append("Message" + Environment.NewLine);
sbExceptionMessage.Append(exception.Message + Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
////Calling this twice is a problem. How can I rebuild this to log both into the appropriate column?////
LogToDB(sbExceptionMessage.ToString());
LogToDB(sbExceptionType.ToString());
}
private static void LogToDB(string log)
{
string connectionString = ConfigurationManager.ConnectionStrings["redacted"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("redacted", con);
cmd.CommandType = CommandType.StoredProcedure;
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", log) { },
new SqlParameter("@exceptionMessage", log) { }
};
cmd.Parameters.AddRange(sp.ToArray());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
调用LogToDb
两次导致向数据库中插入2 行。
将 log
作为 @exceptionType
和 @exceptionMessage
参数值传递会导致在 Type
和 Message
列中插入相同的值。
修改LogToDB
为
private static void LogToDB(string exceptionType, string exceptionMessage)
{
....
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", exceptionType) { },
new SqlParameter("@exceptionMessage", exceptionMessage) { }
};
...
}
并且只调用LogToDB
一次(而不是像现在那样调用两次)
LogToDB(sbExceptionType.ToString(), sbExceptionMessage.ToString());
我正在按照 KudVenkat
的教程将异常记录到 .NET
中的数据库:http://csharp-video-tutorials.blogspot.com/2012/12/logging-exception-to-database-part-75.html
他的方法很适合存储一件事——一个包含异常消息、异常类型、堆栈跟踪等的 ExceptionMessage 字符串——到一列——@exceptionMessage
列。
我试图将这个完整的异常消息分成几个部分,例如 ExceptionType(到 @exceptionType 列)、exceptionMessage(到 @exceptionMessage 列)、StackTrace(到 @stackTrace 列)等。
到目前为止,我可以使用以下方法传入多个 SqlParameters
:
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", log) { },
new SqlParameter("@exceptionMessage", log) { }
};
cmd.Parameters.AddRange(sp.ToArray());
但我似乎无法将这些数据拆分成我想要的列 - 我这样做的方式是,我必须为每个 SqlParameters 调用我的记录器函数(请参阅我的下面的代码注释),它们各自创建一个新行,每一列上都有相同的消息。它不是 Type
转到类型列,Message
转到消息列,而是创建一行 Message
插入类型和消息列,然后是另一行 Type
插入类型和消息列。
我认为这与我的 LogToDB 的性质有关 class:
public class storedProcedure : ApiController
{
public static void Log(Exception exception)
{
// exception type
StringBuilder sbExceptionType = new StringBuilder();
// exception message
StringBuilder sbExceptionMessage = new StringBuilder();
do
{
// exception type
sbExceptionType.Append("Exception Type" + Environment.NewLine);
sbExceptionType.Append(exception.GetType().Name);
sbExceptionType.Append(Environment.NewLine + Environment.NewLine);
// exception message
sbExceptionMessage.Append("Message" + Environment.NewLine);
sbExceptionMessage.Append(exception.Message + Environment.NewLine + Environment.NewLine);
sbExceptionMessage.Append("Stack Trace" + Environment.NewLine);
sbExceptionMessage.Append(exception.StackTrace + Environment.NewLine + Environment.NewLine);
exception = exception.InnerException;
}
while (exception != null);
////Calling this twice is a problem. How can I rebuild this to log both into the appropriate column?////
LogToDB(sbExceptionMessage.ToString());
LogToDB(sbExceptionType.ToString());
}
private static void LogToDB(string log)
{
string connectionString = ConfigurationManager.ConnectionStrings["redacted"].ConnectionString;
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("redacted", con);
cmd.CommandType = CommandType.StoredProcedure;
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", log) { },
new SqlParameter("@exceptionMessage", log) { }
};
cmd.Parameters.AddRange(sp.ToArray());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
调用LogToDb
两次导致向数据库中插入2 行。
将 log
作为 @exceptionType
和 @exceptionMessage
参数值传递会导致在 Type
和 Message
列中插入相同的值。
修改LogToDB
为
private static void LogToDB(string exceptionType, string exceptionMessage)
{
....
List<SqlParameter> sp = new List<SqlParameter>() {
new SqlParameter("@exceptionType", exceptionType) { },
new SqlParameter("@exceptionMessage", exceptionMessage) { }
};
...
}
并且只调用LogToDB
一次(而不是像现在那样调用两次)
LogToDB(sbExceptionType.ToString(), sbExceptionMessage.ToString());