2011-11-22 2 views
-1

전체 예외 및 스택 추적 부분 만 표시하고 싶습니다. 현재 예외에 대한 ex.ToString()을 호출하고 있지만 stacktrace 항목이 "중복"되었음을 확인했습니다.예외 스택 트레이스에서 '중복'메소드 이름을 제거하려면 어떻게해야합니까?

내가 생각할 수있는 유일한 점은 라인 #을 무시하고 중복 문자열에 대한 문자열을 구문 분석하는 것입니다. 나는 정규식 패턴을 알아 내야 할 것 같다.

예는 : 스택 트레이스에 "ProcessTeleformFiles.ProcessExpTableRecord이 (..."두 번하지만 서로 다른 라인 #의 's의 나열이

내가 쓰는 거의 모든 단일 C# 4.0의 방법으로 시도의 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 

UPDATE : 그래서 돌아 오는 길에 , 내 코드에서 모든 시도 어획량을 제거했다. stacktrace를 수정하는 유일한 상황은 사용자에게 좀 더 사용자 친화적 인 단순화 된 출력을 제공하는 한 곳에서입니다. 내부적으로 항상 전체 스택을 기록합니다. 모두에게 감사드립니다!

+0

무엇을하려고합니까? 이들은 '중복'하지 않습니다. 스택의 현재 프레임입니다. 왜 이걸하고 싶어? – linuxuser27

+0

메시지 문자열이 더 짧아지고 더 읽기 쉽고 유용 할 것으로 생각하기 때문에. – user610064

+2

왜 더 짧게하고 싶습니까? 짧아지면 불완전 해져서 문제의 원인을 찾기가 더 어려워집니다! –

답변

3

스택 추적의 추가 행은 "중복"되지 않으며 실행 흐름을 나타냅니다.이 경우 예외가 발견되어 다시 처리됩니다. 당신이 그 (것)들을 제거하는 경우에 당신은 무슨 일이 일어 났느가에 관하여 귀중한 정보를 제거하고있다. 언젠가는 당신이 오류를 디버그하려고 할 때 자신이 상처를 입히고 일어난 일을 해결할 수 없다.

해결책은 간단합니다. 은 try-catch 블록에 각 방법을 래핑하지 않습니다. 스택 추적을 복잡하게하고, 예외가 발견되어 다시 throw 될 때마다 성능 저하를 초래하고 특정 예외 유형을 잡는 것을 매우 어렵게 만듭니다.

각 메소드에서 예외를 포착하고 재실행하는 데 전혀 이점이 없습니다. 오류 메시지를 표시하여 처리하는 "루트 레벨"에있는 유용한 정보를 얻을 수있을 때이를 catch하면됩니다.

+1

@user :이 사실을 충분히 강조 할 수는 없습니다. 모든 방법을 시도하거나 잡을 필요가 있다고 생각하기 때문에 응용 프로그램을 망칠 것입니다.그것은 정확하게 잘못된 행동이며 문제 해결의 삶을 악몽으로 만들 것입니다. 거의 try/catch로 코드를 둘러 쌀 수 없습니다. –

+0

try catch를 사용하여 데이터베이스 레코드 (Databasename, tablename, 레코드 ID)를 반복하면서 오류가있는 위치를 정확하게 추적 할 수 있어야하므로 새로운 예외 ("테이블 이름 xxx", 예) 이론을 사용합니다. . 빈 시도 캐치를 제거하고 작동 방법을 확인합니다. 지금 나는 많은 많은 방법에서 try catch 블록을 제거하기위한 창조적 인 정규식을 생각해 낼 것입니다. – user610064

+0

@ user610064 추가 정보 (예 : 루프에서 처리되는 항목의 이름)를 추가하기 위해 예외를 포착 한 다음 래핑/재발행하는 것은 좋으며 일반적으로 추가 작업이없는 모든 장소에서 캐치 및 래핑 예외가 발생합니다 귀하의 문제를 일으키는 정보 – Justin

1

스택 트레이스에서 항목을 제거하면 더 이상 스택 트레이스가 아닙니다! 정확히 어떤 함수가 호출되었는지를 보여 주며, 함수가 스스로를 호출하면 주목할만한 정보입니다!

관련 문제