如何从异常堆栈跟踪中删除“重复”方法名称?

时间:2011-11-22 15:54:02

标签: c#

我想只显示完整异常和堆栈跟踪的一部分。 我目前正在调用异常的ex.ToString(),但我注意到有“重复”的堆栈跟踪条目。

我唯一能想到的就是解析字符串中重复的字符串而忽略了#行。我想我必须找出一个正则表达式模式。

例如:在堆栈跟踪中,“ProcessTeleformFiles.ProcessExpTableRecord(...”列出两次,但行号不同。

我在几乎每一个我编写的c#4.0方法中都使用了try catch块。

try
catch (Exception)
{
  throw;
}

然后当我想提出自定义错误时,我会这样做

try 
catch (Exception ex)
{
  throw new Exception("my custom message", ex);
}

然后在代码的根级别我基本上显示和/或记录错误:

try 
catch (Exception ex)
{
  MessageBox.Show("Exception found: " + ex.ToString();
}

然而,堆栈跟踪看起来像这样:

Error was logged: Error in ProcessExp() method.
Exception found: System.Exception: Error found in Database: C:\MyDatabase.mdb
 ---> System.Exception: Error found in Table: MyTable
 ---> System.Exception: Error found in TableID: 1
 ---> System.Data.OleDb.OleDbException: Could not find output table 'testtable'.
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   at System.Data.OleDb.OleDbCommand.ExecuteScalar()
   at MiscOleDbFns.Querys.RunExecuteScaler(OleDbConnection Conn, String QueryString, Object[] ParamValues) in C:\Document Files\DotNet common libraries\src 4.0\misclibrary_src\source\MiscOleDbFns.cs:line 247
   at MiscOleDbFns.Querys.RunExecuteScalerAndGetAutoNumFromQuery(OleDbConnection Conn, String QueryString, Object[] ParamValues) in C:\Document Files\DotNet common libraries\src 4.0\misclibrary_src\source\MiscOleDbFns.cs:line 271
   at ProcessTeleformFiles.InsertExpSummaryFilesRec(OleDbConnection Conn, Boolean LogFile, RcdExpSummaryFiles Rcd) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 622
   at ProcessTeleformFiles.FindFileInList(OleDbConnection conSystemDB, String sCopyToDBFolderExp, Boolean isLogFile, Boolean isCopyFile, Boolean isDeleteFile, RcdExpSummaryFiles recExpSummaryFiles, Dictionary`2 myList) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 1216
   at ProcessTeleformFiles.ProcessExpTableRecordSusFiles(OleDbConnection conSystemDB, String sCopyToDBFolderExp, OleDbDataReader Read, RcdSchemaFlds recSchemaFlds, RcdExpSummary recExpSummary, String& tct) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 1257
   at ProcessTeleformFiles.ProcessExpTableRecord(OleDbConnection conSystemDB, String sCopyToDBFolderExp, OleDbDataReader Read, RcdSchemaFlds recSchemaFlds, RcdExpSummary recExpSummary) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 965
   --- End of inner exception stack trace ---
   at ProcessTeleformFiles.ProcessExpTableRecord(OleDbConnection conSystemDB, String sCopyToDBFolderExp, OleDbDataReader Read, RcdSchemaFlds recSchemaFlds, RcdExpSummary recExpSummary) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 982
   at ProcessTeleformFiles.ProcessExpDatabaseTable(OleDbConnection conSystemDB, OleDbConnection ConnExpDb, String sCopyToDBFolderExp, String mdbPath, String sTable, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 242
   --- End of inner exception stack trace ---
   at ProcessTeleformFiles.ProcessExpDatabaseTable(OleDbConnection conSystemDB, OleDbConnection ConnExpDb, String sCopyToDBFolderExp, String mdbPath, String sTable, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 250
   at ProcessTeleformFiles.ProcessExpDatabase(OleDbConnection conSystemDB, String mdbPath, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 161
   --- End of inner exception stack trace ---
   at ProcessTeleformFiles.ProcessExpDatabase(OleDbConnection conSystemDB, String mdbPath, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 174
   at ProcessTeleformFiles.ProcessExp(BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 74

我希望错误消息没有任何重复的消息;更像这样:

Error was logged: Error in ProcessExp() method.
Exception found: System.Exception: Error found in Database: C:\MyDatabase.mdb
 ---> System.Exception: Error found in Table: MyTable
 ---> System.Exception: Error found in TableID: 1
 ---> System.Data.OleDb.OleDbException: Could not find output table 'testtable'.

at MiscOleDbFns.Querys.RunExecuteScaler(OleDbConnection Conn, String QueryString, Object[] ParamValues) in C:\Document Files\DotNet common libraries\src 4.0\misclibrary_src\source\MiscOleDbFns.cs:line 247
   at MiscOleDbFns.Querys.RunExecuteScalerAndGetAutoNumFromQuery(OleDbConnection Conn, String QueryString, Object[] ParamValues) in C:\Document Files\DotNet common libraries\src 4.0\misclibrary_src\source\MiscOleDbFns.cs:line 271
   at ProcessTeleformFiles.InsertExpSummaryFilesRec(OleDbConnection Conn, Boolean LogFile, RcdExpSummaryFiles Rcd) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 622
   at ProcessTeleformFiles.FindFileInList(OleDbConnection conSystemDB, String sCopyToDBFolderExp, Boolean isLogFile, Boolean isCopyFile, Boolean isDeleteFile, RcdExpSummaryFiles recExpSummaryFiles, Dictionary`2 myList) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 1216
   at ProcessTeleformFiles.ProcessExpTableRecordSusFiles(OleDbConnection conSystemDB, String sCopyToDBFolderExp, OleDbDataReader Read, RcdSchemaFlds recSchemaFlds, RcdExpSummary recExpSummary, String& tct) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 1257
   at ProcessTeleformFiles.ProcessExpTableRecord(OleDbConnection conSystemDB, String sCopyToDBFolderExp, OleDbDataReader Read, RcdSchemaFlds recSchemaFlds, RcdExpSummary recExpSummary) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 965
at ProcessTeleformFiles.ProcessExpDatabaseTable(OleDbConnection conSystemDB, OleDbConnection ConnExpDb, String sCopyToDBFolderExp, String mdbPath, String sTable, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 242
at ProcessTeleformFiles.ProcessExpDatabase(OleDbConnection conSystemDB, String mdbPath, BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 161
at ProcessTeleformFiles.ProcessExp(BackgroundWorker worker, DoWorkEventArgs workerEvents) in C:\Document Files\Projects and notes\VS 2010\Projects\OrganizeTeleformFiles\source\OrganizeTeleformFiles\ProcessFiles.cs:line 74

更新: 回过头来的时候,我确实删除了代码中的所有try catch。我现在修改堆栈跟踪的唯一情况是在一个地方,我给用户一个更加用户友好的简化输出。在内部我总是记录完整的堆栈。谢谢大家!

2 个答案:

答案 0 :(得分:3)

堆栈跟踪中的额外行不是“重复”,它们表示执行流程 - 在这种情况下捕获并重新抛出异常。如果你删除它们,那么你正在删除有关发生的事情的有价值的信息,当你试图调试错误并且无法解决发生的事情时,有一天会让你自己受到伤害。

解决方案很简单 - 不要将每个方法包装在try-catch块中。它使堆栈跟踪变得复杂(如您所见),每次捕获并重新抛出异常时都会导致性能下降,并且很难捕获特定的异常类型。

在每个方法中捕获和重新抛出异常绝对没有优势 - 只要在你可以使用它做一些有用的事情时就抓住它,在你的情况下,你可以通过显示错误消息处理它的“根级别”。 / p>

答案 1 :(得分:1)

如果从堆栈跟踪中删除条目,则不再是堆栈跟踪!它准确显示了调用了哪个函数,当函数调用自身时,这是一个值得注意的信息!