C# 程序崩溃而不显示错误时如何识别问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/798239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:36:24  来源:igfitidea点击:

How to identify problem when program crashes without showing error?

c#debuggingcrash-dumps

提问by Josh

Please let me know what steps I need to follow when my application crashes and closes showing the dialog containing "Don't send" and "Send error report" buttons.

请让我知道当我的应用程序崩溃并关闭显示包含“不发送”和“发送错误报告”按钮的对话框时我需要遵循哪些步骤。

What can I possibly do other than looking at the event viewer to solve this?

除了查看事件查看器来解决这个问题,我还能做什么?

Thanks

谢谢

采纳答案by Groo

  1. You could add a try/catch/finallyconstruct around your Main()entry method's body.

  2. For WinForms, you can add a ThreadExceptionhandler, just before Application.Run(), to catch exceptions thrown in WinForms UI event handlers:

    Application.ThreadException +=
       new ThreadExceptionEventHandler(Application_ThreadException);
    
  3. All other unhandled exceptions can be caught using:

    AppDomain.CurrentDomain.UnhandledException +=
       new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    

    But it's worth mentioning that this only allows you to log/report the exception - you cannot prevent the application from closing once you exit this final handler.

  4. Visual Studio can also be configured to break on first chance exceptions, and external debuggers (like WinDbg with managed SoS extensions) can catch first-chance exceptions too (http://www.codeproject.com/KB/debug/windbg_part1.aspx).

  1. 您可以try/catch/finallyMain()输入方法的主体周围添加一个构造。

  2. 对于 WinForms,您可以ThreadException在 Application.Run() 之前添加一个处理程序,以捕获 WinForms UI 事件处理程序中抛出的异常:

    Application.ThreadException +=
       new ThreadExceptionEventHandler(Application_ThreadException);
    
  3. 可以使用以下方法捕获所有其他未处理的异常:

    AppDomain.CurrentDomain.UnhandledException +=
       new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    

    但值得一提的是,这只允许您记录/报告异常 - 一旦退出此最终处理程序,您就无法阻止应用程序关闭。

  4. Visual Studio 还可以配置为在第一次机会异常时中断,并且外部调试器(如带有托管 SoS 扩展的 WinDbg)也可以捕获第一次机会异常(http://www.codeproject.com/KB/debug/windbg_part1.aspx) .

Also, use a logging framework like log4net to add useful logging to your app and dump exception information before the application closes.

此外,使用 log4net 之类的日志记录框架向您的应用程序添加有用的日志记录并在应用程序关闭之前转储异常信息。

回答by Tony

Ask your users if they can reproduce the error and how. If you can reproduce the error, run in debug in Visual Studio and follow the steps to cause the crash. Visual studio will enter debug mode where it catches the error. Form there you will be able to follow the stack trace and see what code is causing the error. Visual studio makes debugging pretty easy most of the time.

询问您的用户是否可以重现错误以及如何重现。如果可以重现该错误,请在 Visual Studio 中的调试中运行并按照导致崩溃的步骤进行操作。Visual Studio 将进入调试模式以捕获错误。在那里,您将能够跟踪堆栈跟踪并查看导致错误的代码。大多数情况下,Visual Studio 使调试变得非常容易。

回答by RSlaughter

Ideally you should use a logging library such as nLogor log4netto log any unhandled exceptions, and exceptions in general, by logging them in your code when they occur.

理想情况下,您应该使用诸如nLoglog4net 之类的日志记录库来记录任何未处理的异常和一般的异常,通过在它们发生时将它们记录在您的代码中。

It can also help to have different levels of logging in your application to help you track down a problem when it's not running on your development machine. With nLog you can leave the logging in your production code and enable / disable log output through the use of a logging config file.

当应用程序未在开发机器上运行时,它也可以帮助您在应用程序中使用不同级别的日志记录来帮助您跟踪问题。使用 nLog,您可以将日志记录保留在生产代码中,并通过使用日志记录配置文件启用/禁用日志输出。

I've not used log4net so I don't know if it has a similar feature.

我没有使用过 log4net,所以我不知道它是否有类似的功能。

回答by Jon B

The "send/don't send" errors tend to happen when you have an unhandled exception in a background thread (the main thread will show that continue/quit .NET dialog with a stack trace).

当后台线程中出现未处理的异常时,往往会发生“发送/不发送”错误(主线程将显示带有堆栈跟踪的继续/退出 .NET 对话框)。

Add an exception handler to your thread's function and log from there:

将异常处理程序添加到线程的函数并从那里记录:

void RunMyThread()
{
    try
    {
        // background thread code
    }
    catch (Exception ex)
    {
        // Log the exception
    }
}

This is highly simplified, and may not be how you want to handle an exception. But hopefully this will get you moving in the right direction.

这是高度简化的,可能不是您想要处理异常的方式。但希望这会让你朝着正确的方向前进。

回答by Krzysztof Kozmic

Use WinDBG to debug the issue. You can make it break (as in stop on a breakpoint) when an exception is thrown, and then examine the stacktrace... objects in scope etc...

使用 WinDBG 调试问题。您可以在抛出异常时使其中断(如在断点处停止),然后检查堆栈跟踪...范围内的对象等...

回答by pj4533

If it happens at a customer site, and isn't easily reproducable inside a developers debugger, you could do some post mortem debugging. I like to use Userdumpto gather a memory dump file (.DMP). Then I use windbg for analysis.

如果它发生在客户站点,并且在开发人员调试器中不容易重现,您可以进行一些事后调试。我喜欢使用Userdump来收集内存转储文件 (.DMP)。然后我使用windbg进行分析。