我想只显示部分完整的异常和堆栈跟踪。 我目前在异常上调用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: 所以回来的路上时,我并删除所有尝试捕获在我的代码。我现在修改堆栈跟踪的唯一情况是在一个地方,我给用户提供了一个更简便的用户界面输出。在内部,我总是记录整个堆栈。感谢大家!
你想做什么?这些不是'重复的'。它们是堆栈中的当前帧。你为什么想这样做? – linuxuser27
因为我认为消息字符串会更短,更具可读性,因此非常有用。 – user610064
你为什么要把它缩短?如果它更短,它将不完整,这将使它更难找到问题的原因! –