我正在尝试捕获异常的内部异常并将其记录到 SQL 数据库表中。我的代码毫无问题地捕获并记录了 SQL 表的异常,但即使异常消息显示“有关详细信息,请参阅内部异常”,我也从未获得内部异常记录。就捕获内部异常而言,有人可以在这里指出正确的方向吗?这是捕获异常但不适用于内部异常的代码。
try
{
context.CalculateFreight(xmldoc);
}
catch (Exception ex)
{
if (ex.Message.ToString().Length > 2048)
{
ApplicationError myError = new ApplicationError();
myError.ErrorNumber = 1;
myError.ErrorSeverity = 1;
myError.ErrorState = 1;
myError.ErrorLine = 29;
myError.ErrorProcedure = "Web Service - ProcessShipping()";
myError.ErrorMessage = ex.Message.ToString().Substring(0, 2047);
myError.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myError);
context.SaveChanges();
}
else
{
ApplicationError myError = new ApplicationError();
myError.ErrorNumber = 1;
myError.ErrorSeverity = 1;
myError.ErrorState = 1;
myError.ErrorLine = 29;
myError.ErrorProcedure = "Web Service - ProcessShipping()";
myError.ErrorMessage = ex.Message.ToString();
myError.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myError);
context.SaveChanges();
}
if (ex.InnerException.Message.ToString().Length > 0)
{
if (ex.InnerException.Message.ToString().Length > 2048)
{
ApplicationError myInnerException = new ApplicationError();
myInnerException.ErrorNumber = 1;
myInnerException.ErrorSeverity = 1;
myInnerException.ErrorState = 1;
myInnerException.ErrorLine = 29;
myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
myInnerException.ErrorMessage = ex.InnerException.Message.ToString().Substring(0, 2047);
myInnerException.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myInnerException);
context.SaveChanges();
}
else
{
ApplicationError myInnerException = new ApplicationError();
myInnerException.ErrorNumber = 1;
myInnerException.ErrorSeverity = 1;
myInnerException.ErrorState = 1;
myInnerException.ErrorLine = 29;
myInnerException.ErrorProcedure = "Web Service - INNER EXCEPTION";
myInnerException.ErrorMessage = ex.InnerException.Message.ToString();
myInnerException.ErrorDateTime = DateTime.Now;
context.ApplicationErrors.AddObject(myInnerException);
context.SaveChanges();
}
}
}