捕获 C# .NET 中的所有崩溃
Stack Overflow 上已经有一些关于这个主题的非常好的帖子,但似乎没有一个关于其中任何一个的简洁答案。我的 C# 控制台应用程序(作为 Windows 服务运行)启动一个 Java 进程并管理它(启动/停止/重新启动它),但我的问题是我将远程访问计算机,有时会看到它启动了大约 20 个 Java 进程。
这显然是我的应用程序在某个时刻崩溃并且没有关闭它启动的 Java 进程的问题。我在 AppDomain.CurrentDomain 中挂接了“UnhandledExceptionEventHandler”,并从中调用 TerminateProcess() (关闭活动的 Java 进程),但此问题仍然偶尔发生。
我的应用程序有主线程、TCP 服务器线程(接受异步连接)和 UDP 服务器线程。在 UnhandledException 之上还有什么我应该挂钩的吗?
编辑
我还突然想到,我的代码中有一些 Try/Catch 块,它们只是写入控制台,但我从未见过。我应该删除它们,以便它们被 UnhandledException 捕获,还是添加一个记录器?
There are already some pretty good threads on this topic on Stack Overflow, but there doesn't really seem to be a concise answer on any of them. My C# console application (running as a Windows service) launches a Java process and manages it (starts/stops/restarts it), but my issue is that I will remote into machines, and see it has started about 20 Java processes sometimes.
This is obviously an issue with my application crashing at some point, and not shutting down the Java process it started. I have hooked "UnhandledExceptionEventHandler" in the AppDomain.CurrentDomain, and I call TerminateProcess() from it (shuts down the active Java process) but this issue is still occuring on occassion.
My application has the Main thread, a TCP Server Thread (which accepts async connections), and a UDP Server Thread. Is there anything else I should be hooking into on top of UnhandledException?
EDIT
It also just occured to me that I have a few Try/Catch blocks in my code that simply write to console, which I never see. Should I just remove these so these are caught by the UnhandledException or add a logger there instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,如果您不想记录日志,则必须将代码中的
Console.WriteLine..
行更改为Debug.WriteLine..
,因此输出它只会处于调试状态。其次,当发生任何异常时,如果您不知道如何处理或修复它,则重新抛出它
catch { throw; }
登录后。我个人认为在清理代码后,现在只要抛出
DomainUnhandledException
,就可以重新启动应用程序。 此处有一个示例,其想法是从您的应用程序,然后终止第一个应用程序。您还定义了一个互斥体,因此一次只有一个实例处于活动状态。First of all you have to change the
Console.WriteLine..
lines in you code toDebug.WriteLine..
if you don't want to log, so the output of it will only be on debug.Second when any exception occurs if you don't know how to handle it or fix it then rethrow it
catch { throw; }
after logging. I personally doAfter you cleaning up you code, now you can whenever
DomainUnhandledException
thrown, you can restart your application. an example will be here, The idea is you start a new instance from your application and then terminate the first one. you also define a mutex so only one instance at time will be alive.需要考虑的是您是否希望 .NET 应用程序负责生成的进程。如果可能的话,他们可能会负责在不再接收输入时关闭。如果生成的进程在其他计算机上运行,我无论如何都会尝试这样做,因为网络问题可能会干扰从 .NET 应用程序向 Java 进程发送关闭消息。每个人都有自己的责任。
也就是说,修复 .NET 应用程序中的异常处理(特别是当您丢失一些异常时)也很重要。让每个线程对其异常负责,并确保记录它们以便于调试。
Something to consider is whether you want the .NET application to be responsible for the spawned processes. If possible, they might be made responsible for shutting down when no longer receiving input. If the spawned processes are running on other machines I would try to do that anyway because network problems might interfere with sending a shutdown message from the .NET application to the Java processes. Each its own responsibilities.
That said, getting exception handling in the .NET application fixed (especially when you are missing some exceptions) is also important. Make each thread responsible for its exceptions and make sure you log them for easy debugging.