获取堆栈跟踪中的参数值?

时间:2013-01-19 04:20:19

标签: .net vb.net

在以下代码中,Inner_A(x)中抛出异常。如何在堆栈跟踪中显示x的值是什么,因为堆栈跟踪如下所示:

at Default2.Inner_A(String x) in C:\test1\Default2.aspx.vb:line 25
at Default2.Page_Load(Object sender, EventArgs e) in C:\test1\Default2.aspx.vb:line 12






Dim strErrorMesssage As String

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
        Dim x As String = "testvar"

        Inner_A(x)
    Catch ex As Exception

        strErrorMesssage &= DirectCast(ex, System.ApplicationException).StackTrace.ToString()

    End Try


End Sub


Private Function Inner_A(ByVal x As String) As String

    Throw New ApplicationException("Exception Occured caused in INNER_A ")

    Return " Function Inner_A"

End Function

1 个答案:

答案 0 :(得分:1)

您无法修改堆栈跟踪,但可以在错误消息中包含该变量的值:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim x As String
    Try
        x = "testvar"

        Inner_A(x)
    Catch ex As Exception
        strErrorMesssage &= "X = " & x & Environment.NewLine & DirectCast(ex, System.ApplicationException).StackTrace.ToString()
    End Try
End Sub