如何删除'duplicate'异常堆栈跟踪中的方法名
本文关键字:跟踪 堆栈 方法 异常 duplicate 删除 何删除 | 更新日期: 2023-09-27 18:15:20
我想只显示完整异常和堆栈跟踪的一部分。我目前正在异常上调用ex.ToString(),但我注意到有"重复"的stacktrace条目。
我能想到的唯一方法就是解析字符串中重复的字符串,忽略行#。我想我必须找出一个正则表达式模式。
。:在堆栈跟踪中有"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捕获。我现在修改堆栈跟踪的唯一情况是在一个地方,我向用户提供了一个更友好的简化输出。在内部,我总是记录整个堆栈。谢谢大家!
堆栈跟踪中的额外行不是"重复的",它们表示执行流程——在这种情况下,异常被捕获并重新抛出。如果你删除了它们,那么你就删除了关于发生了什么事情的有价值的信息,当你试图调试错误却无法弄清楚发生了什么事情时,这些信息总有一天会给你带来很大的伤害。
解决方案很简单- 不要将每个方法包装在try-catch块中。它使堆栈跟踪变得复杂(正如您所看到的),每次捕获和重新抛出异常时都会导致性能损失,并且使捕获特定异常类型变得非常困难。
在每个方法中捕获和重新抛出异常绝对没有任何好处——只有在你可以用它做一些有用的事情时才捕获它,在你的情况下是在"根级",你通过显示错误消息来处理它。
如果你从堆栈跟踪中删除条目,它就不再是堆栈跟踪了!它准确地显示了哪个函数被调用,当一个函数调用自己,那么这是一个值得注意的信息!