gpt4 book ai didi

.net - 有意义地精简异常/Environment.StackTrace

转载 作者:行者123 更新时间:2023-12-02 00:31:37 30 4
gpt4 key购买 nike

我正在分析遗留应用程序中的错误,同时清理并改进它。我有一些堆栈跟踪被记录到数据库中,但是它存储的量有一个限制 (VARCHAR2(1000)),并且错误发生在 System.Data 的深处......

   at System.Data.OracleClient.OracleConnection.CheckError(OciErrorHandle errorHandle, Int32 rc)
at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, Boolean needRowid, OciRowidDescriptor& rowidDescriptor, ArrayList& resultParameterOrdinals)
at System.Data.OracleClient.OracleCommand.Execute(OciStatementHandle statementHandle, CommandBehavior behavior, ArrayList& resultParameterOrdinals)
at System.Data.OracleClient.OracleCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.OracleClient.OracleCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)

我不关心所有这些东西——我只需要找到产生错误的 C# 代码和过程。因此,我可以找出根本原因,重构代码以提高质量,并在找到最容易出错的代码时改进每个模块。

在我开始写东西之前,有人有什么东西可以将堆栈跟踪限制到某些程序集,这样我就可以找到根本原因吗?还是堆栈跟踪解析器或现成的东西,我可以用它在记录错误之前去除我不感兴趣的东西?

最佳答案

减少 .Net 异常的代码

您很幸运 - 已经有人阅读了您的帖子!我想我已经通过将我的错误重新格式化成这样的东西来避免阅读至少一本史蒂文金小说的值(value):

(

 不尊重我的制表位所以它在现实生活中看起来更好)

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
FUNCTION FILE LINE
CoCreateInstance (no .PDB)
CreateWithoutLicense (no .PDB)
CreateWithLicense (no .PDB)
CreateInstanceCore (no .PDB)
CreateInstance (no .PDB)
GetOcxCreate (no .PDB)
TransitionUpTo (no .PDB)
CreateHandle (no .PDB)
CreateControl (no .PDB)
CreateControl (no .PDB)
EndInit (no .PDB)
InitializeComponent frmGetSig.Designer.vb 187
ctor frmGetSig.vb 264
btnSign_Click ucSignCapture_v2.vb 306

使用这段代码:

Public Shared Function ReduceError(ByVal stExceptionToString As String) As String
Try
''// split into message and stack trace items
Dim stMainSplit() As String = Split(stExceptionToString, vbCrLf & " at ")
If UBound(stMainSplit) = 0 Then
Return stExceptionToString
End If
''// add the message
Dim stResults As String = stMainSplit(0) & vbCrLf
Dim bLastWasInner As Boolean
''// Headings, if there is a stack trace
If UBound(stMainSplit) > 0 Then
stResults &= "FUNCTION" & vbTab & "FILE" & vbTab & "LINE" & vbCrLf
End If
''// reduce the stack trace
For i As Integer = 1 To UBound(stMainSplit)
Dim st As String = stMainSplit(i)
''// skip the line following inner exception in stack trace
If Not bLastWasInner Then
''// split the sub from line # info
Dim stln() As String = Split(st, ")")
If UBound(stln) = 1 Then
''// first put the sub on there
Dim stsub As String = Mid(stln(0), 1, InStrRev(stln(0), "(") - 1)
If InStr(stsub, ".") <> 0 Then
stsub = Mid(stsub, InStrRev(stsub, ".") + 1)
End If
stResults &= stsub & vbTab
''// Now if there is file/line # info, add that.
Dim stFile As String = "(no .PDB)"
If 0 <> InStr(stln(1), "\") Then '' " //good grief SOF learn vb ;-)
stFile = Replace(Mid(stln(1), InStrRev(stln(1), "\") + 1), ":line ", vbTab) '' "
''// take off CR and 'inner exception..." text
If InStr(stFile, vbCrLf) > 1 Then
stFile = Mid(stFile, 1, InStr(stFile, vbCrLf) - 1)
End If
End If
stResults &= stFile & vbCrLf
Else
stResults &= "Line split by ) has no (" & vbTab & vbCrLf
End If
End If
If 0 <> InStr(st, "End of inner exception stack trace") Then
bLastWasInner = True
Else
bLastWasInner = False
End If
Next
Return stResults
Catch ex As Exception
''// might want to return orig error here as well..
Return "Error summarizing routine as error'd: " & ex.ToString
End Try
End Function

它放入了我在文本框中使用的选项卡,如果需要,可以很容易地更改;如果您不关心 system.data 行,您总是可以将它们排除在外。为 .net 1.1 编写,应该很好地转换为 c#。

关于.net - 有意义地精简异常/Environment.StackTrace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6472314/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com