2011-11-22 60 views
-1

我想只显示部分完整的异常和堆栈跟踪。 我目前在异常上调用ex.ToString(),但我注意到有“重复”堆栈跟踪条目。如何从异常堆栈跟踪中移除“重复”方法名称?

我能想到的唯一办法是解析字符串,忽略行#的重复字符串。我想我将不得不找出一个正则表达式模式。

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

我使用try catch块在几乎每一个C#4.0的方法我写的。

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 

UPDATE: 所以回来的路上时,我并删除所有尝试捕获在我的代码。我现在修改堆栈跟踪的唯一情况是在一个地方,我给用户提供了一个更简便的用户界面输出。在内部,我总是记录整个堆栈。感谢大家!

+0

你想做什么?这些不是'重复的'。它们是堆栈中的当前帧。你为什么想这样做? – linuxuser27

+0

因为我认为消息字符串会更短,更具可读性,因此非常有用。 – user610064

+2

你为什么要把它缩短?如果它更短,它将不完整,这将使它更难找到问题的原因! –

回答

3

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

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

在每种方法中捕获和重新抛出异常绝对没有什么好处 - 只要在可以对其执行某些有用的操作时捕获并重新抛出异常,您的情况就是在“根级”,通过显示错误消息来处理异常。

+1

@user:这个不能被强调。显然,你正在破坏你的应用程序,因为你认为你需要每种方法的尝试/抓住。这恰恰是错误的做法,并会使您的故障排除生活成为一场噩梦。你几乎不应该用try/catch来包围代码。 –

+0

我需要使用try catch来帮助我确切地跟踪错误的位置,因为我通过数据库记录(Databasename,tablename,record id)循环,因此我使用throw new Exception(“table name xxx”,ex)理论。 我将删除空的尝试捕捉,看看它是如何工作的。现在我想我必须想出一些创造性的正则表达式来从许多方法中删除try catch块。 – user610064

+0

@ user610064捕捉然后包装/重新抛出异常以添加其他细节(例如循环中正在处理的项目的名称)是很好的常见做法,它在所有没有附加条件的地方捕捉和包装异常信息是造成你的问题 – Justin

1

如果从堆栈中删除条目,它不再是堆栈跟踪!它显示了确切地调用了哪个函数,以及函数何时调用它自己,这是一个值得注意的信息!

相关问题