如何监视 .Net 服务器应用程序的致命异常?
我正在开发一个 .Net 服务器应用程序,它应该连续运行。我希望每次服务器进程因任何原因终止时都会发送一封通知电子邮件。理想情况下,如果异常导致终止,电子邮件应包含异常消息和堆栈跟踪。我知道某些异常无法捕获,例如 StackOverflowException。那么,如何记录/发送服务器进程中发生 StackOverflowException 的通知?
我正在考虑创建第二个进程来监视服务器进程。但是如何获取发生异常的详细信息呢?
I am developing a .Net server application, which is supposed to run continously. I would like to have a notification email sent each time the server process is terminated for any reason. Ideally the email should contain the exception message and stacktrace if an exception caused the termination. I am aware that certain exceptions can not be caught, such as StackOverflowException
. So how would you log / send notification that a StackOverflowException
occured in the server process?
I am thinking along the lines of creating a second process to monitor the server process. But how would that get the details of the exceptions occuring?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StackOverflowException 等致命异常通常会在 Windows 事件日志中生成一个条目(甚至可能包括堆栈跟踪)。您可以监控您的事件日志(例如使用其他服务),该日志将针对某些条目发送电子邮件。这里描述了一个用于监视事件日志条目的简单应用程序:
在这里:
如果您希望服务持续启动并运行,您可能需要配置它会在崩溃时自动重新启动(尽管您应该知道这通常是崩溃的原因,并且简单地重新启动不一定会消除该原因)。您可以在服务属性下的恢复选项卡上执行此操作。您还可以选择在服务崩溃时调用自定义程序或脚本。
使用内置 Windows 功能的另一个选项是配置事件日志以发送电子邮件通知。这至少在 Windows Vista 上有效,如下所述:
Fatal exceptions such as a StackOverflowException will usually result in an entry in the Windows event log (maybe even including the stacktrace). You can monitor your event log (e.g. with another service) that will send out an email on certain entries. A simple application for monitoring event log entries is described here:
and here:
If you want your service to be continuously up and running you might want to configure it to restart automatically in case it crashed (although you should be aware that usually is a reason why it crashed and simply restarting does not necessarily remove that reason). You can do so on the Recovery tab under service properties. There you also have the option to call a custom program or script in case of a service crash.
Another option using built-in Windows features is to configure event log to send an email notification. This works at least from Windows Vista on, as described here:
实际上,您根本不需要任何第 3 方软件来执行此操作 - Win7 任务计划程序可以在看到某种类型的事件时触发计划任务,并将未处理的异常写入事件日志。内置操作之一是“发送电子邮件”,因此您可以在操作系统中获得整个解决方案
Actually, you don't need any 3rd party software at all to do this - Win7 Task Scheduler can fire a scheduled task whenever it sees a certain type of event, and an unhandled exception is written to the Event Log. One of the built-in actions is "Send an Email", so you've got your whole solution in the OS
我建议实施健康监测器。拥有一个可以预测和控制的异常日志是很好的,但是您可以使用内置的运行状况监控框架来配置应用程序以捕获无关的异常。
http://msdn.microsoft.com/en-us/library/ms998306。 aspx
https ://web.archive.org/web/20211020102851/https://www.4guysfromrolla.com/articles/031407-1.aspx
I would recommend implementing a health monitor. It's good to have a log for exceptions you might be able to anticipate and control, but you can configure your application to catch the extraneous ones by using the built in health monitoring framework.
http://msdn.microsoft.com/en-us/library/ms998306.aspx
https://web.archive.org/web/20211020102851/https://www.4guysfromrolla.com/articles/031407-1.aspx
我有一个应用程序(显示的代码)可以拦截未处理的异常并显示自定义异常对话框,这可能会有一些帮助吗?
I have an application (code shown) that intercepts unhandled exceptions and displays a custom exception dialog, this may of of some help?