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

ric*_*ard 5 .net c# variables stack-trace

我维护一个应用程序,当应用程序发生错误时,它会向我发送一封电子邮件.我将堆栈跟踪转储到电子邮件中,似乎工作正常.唯一缺少的是变量的.我得到了所有的调用等等,从来没有任何变量.为了将这些变量值转储到电子邮件中,我还缺少什么?

下面是我用来将其转储到电子邮件中的代码:

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());
Run Code Online (Sandbox Code Playgroud)

以下是电子邮件中的结果:

消息:指定的强制转换无效.来源:MyApplication目标站点:Void FindFormAndActivate(MyApplication.MDIParentForm,System.String,System.Object)堆栈跟踪:MyApplication.DtilityClass.FindFormAndActivate(MDIParentForm frmMDIParentForm,String formName,Object参数)MyApplication.DashboardAlerts.NavigateToAssignment()at MyApplication System.Windows上System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)的System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e)中的.DashboardAlerts.utAlerts_MouseClick(Object sender,MouseEventArgs e).在System.Windows.Fornd.NativeWindow的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中的Forms.Control.WndProc(Message&m).回调(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)

编辑

有些答案建议我自己将变量值添加到电子邮件中.我怎么会得到这些价值呢?发送电子邮件的此代码段不在失败的方法中.这是在发生异常时运行的代码.如果没有处理和纠正异常,我会让它冒泡到线程的顶部,Application.ThreadException += new ThreadExceptionEventHandler(HandleError);并且该HandleError方法是进行此电子邮件调用的方法.它不知道导致异常的方法的变量或参数是什么.

Jus*_*gan 3

无需尝试将它们放入堆栈跟踪中,只需将它们添加到您要发送的字符串中即可。

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());
Run Code Online (Sandbox Code Playgroud)

编辑:

由于电子邮件是在变量的范围之外发送的,因此您需要在它们在范围内时收集它们并将它们添加到抛出的异常中。恐怕我对这个ThreadException对象了解不多,但我会创建一个自定义异常来保存这些变量的值。您将无法自动将它们添加到堆栈跟踪中;这并不是它真正的用途。所以,从我的脑海中浮现出来:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

后来,深入你的代码:

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

事实上,您通过将原始异常保留在属性中来保留原始异常OriginalException应该(希望)也保留原始堆栈跟踪。