gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:10:36 24 4
gpt4 key购买 nike

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

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

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());

这是电子邮件中的结果:

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.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

编辑

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

最佳答案

与其尝试将它们放入堆栈跟踪中,不如将它们添加到您要发送的字符串中。

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());

编辑:

由于电子邮件是在变量范围之外发送的,因此您需要在它们在范围内时收集它们并将它们添加到抛出的异常中。恐怕我不太了解 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;
}
}

稍后,深入您的代码:

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

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

关于c# - 如何在堆栈跟踪转储中获取变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6219647/

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