简体   繁体   English

如何在堆栈跟踪转储中获取变量值?

[英]How do I get variable values in my stack trace dump?

I maintain an application that sends me an email when an error occurs in the application.我维护一个应用程序,当应用程序发生错误时,它会向我发送 email。 I dump the stack trace into the email, and it seems to work ok.我将堆栈跟踪转储到 email 中,它似乎工作正常。 The only thing missing is the values of the variables.唯一缺少的是变量的 I get all the calls and such, just never any variables.我接到所有的电话等等,从来没有任何变量。 What am I missing in order to get these variable values dumped into the email too?为了将这些变量值也转储到 email 中,我还缺少什么?

Below is the code I use to dump it into an email:下面是我用来将其转储到 email 的代码:

UtilityClass.SendEmail(shortNTID,
                       "admin@mydomain.com",
                       new string[] { "support@mydomain.com" },
                       "MyApplication error has occured for user: " +
                            shortNTID + " (Main).",
                       "Message: " + ex.Message.ToString() +
                       " Source: " + ex.Source.ToString() +
                       " Target Site: " + ex.TargetSite.ToString() +
                       " Stack Trace: " + ex.StackTrace.ToString());

And here is the result in the email:这是 email 中的结果:

Message: Specified cast is not valid.消息:指定的强制转换无效。 Source: MyApplication Target Site: Void FindFormAndActivate(MyApplication.MDIParentForm, System.String, System.Object) Stack Trace: at MyApplication.UtilityClass.FindFormAndActivate(MDIParentForm frmMDIParentForm, String formName, Object parameter) at MyApplication.DashboardAlerts.NavigateToAssignment() at MyApplication.DashboardAlerts.utAlerts_MouseClick(Object sender, MouseEventArgs e) at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms来源:MyApplication 目标站点:Void FindFormAndActivate(MyApplication.MDIParentForm, System.String, System.Object) 堆栈跟踪:在 MyApplication.DashboardAlerts.NavigateToAssignment() 在 MyApplication.UtilityClass.FindFormAndActivate(MDIParentForm frmMDIParentForm, String formName, Object 参数) .DashboardAlerts.utAlerts_MouseClick(Object sender, MouseEventArgs e) at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows. Forms.Control.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms .Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) .Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

EDIT编辑

Some answers have suggested that I add the variable values myself to the email.一些答案建议我自己将变量值添加到 email 中。 How would I get those values though?我将如何获得这些值? This snippet of code that sends the email is not in the method that is failing.发送 email 的这段代码不在失败的方法中。 This is code that runs any time an exception occurs.这是在发生异常时运行的代码。 If the exception isn't handled and corrected, I let it bubble up to the top of the thread a la Application.ThreadException += new ThreadExceptionEventHandler(HandleError);如果异常没有得到处理和纠正,我让它冒泡到线程的顶部,例如Application.ThreadException += new ThreadExceptionEventHandler(HandleError); and the HandleError method is the one that makes this email call. HandleError方法是进行此 email 调用的方法。 It has no idea what the variables or parameters were for the method that caused the exception.它不知道导致异常的方法的变量或参数是什么。

The StackTrace property is just the call graph at the point the exception occurred. StackTrace属性只是发生异常时的调用图。 If you're interested in the state of arbitrary local variables, you'll have to add them to the email yourself in the exception handler.如果您对任意局部变量的 state 感兴趣,您必须自己在异常处理程序中将它们添加到 email 中。

Update: If you want to communicate variable values through a chain of exceptions, you'll have to insert them into the exception (ie in the message) when you catch it in the appropriate scope.更新:如果你想通过一系列异常来传递变量值,当你在适当的 scope 中捕获它时,你必须将它们插入到异常中(即在消息中)。

Instead of trying to put them in the stack trace, just add them to the string you're sending.与其尝试将它们放在堆栈跟踪中,不如将它们添加到您发送的字符串中。

UtilityClass.SendEmail(shortNTID,
                   "admin@mydomain.com",
                   new string[] { "support@mydomain.com" },
                   "MyApplication error has occured for user: " +
                        shortNTID + " (Main).",
                   "Message: " + ex.Message.ToString() +
                   " Source: " + ex.Source.ToString() +
                   " Target Site: " + ex.TargetSite.ToString() +
                   " Stack Trace: " + ex.StackTrace.ToString() +
                   " MyVar1 Value: " + MyVar1.ToString() +
                   " MyVar2 Value: " + MyVar2.ToString() +
                   " MyVar3 Value: " + MyVar3.ToString());

Edit:编辑:

Since the email is being sent outside the variables' scope, you're going to need to collect them when they're in scope and add them to the exception being thrown.由于 email 是在变量 scope 之外发送的,因此当它们在 scope 中时,您需要收集它们并将它们添加到抛出的异常中。 I'm afraid I don't know much about the ThreadException object, but I'd create a custom exception that holds the values of those variables.恐怕我对ThreadException object 了解不多,但我会创建一个自定义异常来保存这些变量的值。 You won't be able to automagically add them to the stack trace;您将无法自动将它们添加到堆栈跟踪中; that's not really what it's for.那不是它的真正用途。 So, off the top of my head:所以,在我的脑海中:

public class CustomException : Exception
{
    public string MyVar1 { get; private set; }
    public string MyVar2 { get; private set; }
    public Exception OriginalException { get; private set; }

    public CustomException(Exception original, string myVar1, string myVar2)
    {
        MyVar1 = myVar1;
        MyVar2 = myVar2;
        OriginalException = original;
    }
}

Later, deep in your code:稍后,深入您的代码:

try
{
    //code that might throw exceptions
}
catch (Exception e)
{
    throw new CustomException(e, myVar1, myVar2);
}

The fact that you're preserving the original exception by keeping it in the OriginalException property should (hopefully) also preserve the original stack trace.通过将原始异常保留在OriginalException属性中来保留原始异常这一事实应该(希望)也保留原始堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM