如何监视 .Net 服务器应用程序的致命异常?

发布于 2024-08-19 20:11:46 字数 230 浏览 4 评论 0原文

我正在开发一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

゛时过境迁 2024-08-26 20:11:47

StackOverflowException 等致命异常通常会在 Windows 事件日志中生成一个条目(甚至可能包括堆栈跟踪)。您可以监控您的事件日志(例如使用其他服务),该日志将针对某些条目发送电子邮件。这里描述了一个用于监视事件日志条目的简单应用程序:

实时事件日志监控工具

在这里:

将错误写入事件日志时发送电子邮件警报

如果您希望服务持续启动并运行,您可能需要配置它会在崩溃时自动重新启动(尽管您应该知道这通常是崩溃的原因,并且简单地重新启动不一定会消除该原因)。您可以在服务属性下的恢复选项卡上执行此操作。您还可以选择在服务崩溃时调用自定义程序或脚本。

使用内置 Windows 功能的另一个选项是配置事件日志以发送电子邮件通知。这至少在 Windows Vista 上有效,如下所述:

如何设置事件查看器发送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:

A realtime event log monitoring tool

and here:

Send email alerts when errors are written to the event log

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:

How to Setup Event Viewer to Send a Email Notification in Vista

活泼老夫 2024-08-26 20:11:47

实际上,您根本不需要任何第 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

如歌彻婉言 2024-08-26 20:11:47

我建议实施健康监测器。拥有一个可以预测和控制的异常日志是很好的,但是您可以使用内置的运行状况监控框架来配置应用程序以捕获无关的异常。

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

哀由 2024-08-26 20:11:47

我有一个应用程序(显示的代码)可以拦截未处理的异常并显示自定义异常对话框,这可能会有一些帮助吗?

Imports BM.BEAST.Presenters
Imports BM.BEAST.Security
Imports BM.BEAST.Business
Imports System.Threading

Public Class StartUp

    Shared Sub UnhandledException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)

        Dim unhandledException As New UnhandledExceptionView

        unhandledException.uxExceptionDetails.Text = e.Exception.ToString
        unhandledException.ShowDialog()

        If unhandledException.uxRestart.Checked Then
            Application.Restart()
        Else
            Application.Exit()
        End If

    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Entry point
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Shared Sub Main()

        'All unhandled exceptions are handled by the sub UnhandledException()
        'which displays a BEAST exception dialog and gives the user the chance
        'to copy the error to clipboard for pasting into email etc..
        AddHandler Application.ThreadException, AddressOf UnhandledException

        'Application framework not enabled
        My.User.InitializeWithWindowsUser()

        'Users must be authenticated
        If My.User.IsAuthenticated Then

            Dim securityPrincipal As Security.ISecurityPrincipal = Nothing
            Dim applicationController As ApplicationController = Nothing

            Try
                'Custom security principal for use throughout the session
                securityPrincipal = ServiceGateway.Instance.StartUserSession

                AppDomain.CurrentDomain.SetThreadPrincipal(securityPrincipal)
                Thread.CurrentPrincipal = securityPrincipal

                applicationController = applicationController.GetInstance
                applicationController.RegisterNavigator(New ApplicationNavigator)

                'If a user holds more than 1 role they will have to select
                'the role they want applied for use throughout the session
                If securityPrincipal.Roles.Count > 1 Then applicationController.NavigateTo("SelectRoleView")
                applicationController.NavigateTo("ShellView")

            Catch ex As Exceptions.InvalidUserNameException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Invalid User")
            Catch ex As Exceptions.UserDisabledException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "User Disabled")
            Catch ex As System.Net.Sockets.SocketException
                MsgBox("The xxxxx Server is unavailable, contact the System Administrator.", MsgBoxStyle.Exclamation, "Server Unavailable")
            End Try

        Else
            MsgBox("User not authenticated, the application will terminate.", MsgBoxStyle.Exclamation, "Authentication")
        End If

        My.Settings.Save()
        Application.Exit()

    End Sub

End Class

I have an application (code shown) that intercepts unhandled exceptions and displays a custom exception dialog, this may of of some help?

Imports BM.BEAST.Presenters
Imports BM.BEAST.Security
Imports BM.BEAST.Business
Imports System.Threading

Public Class StartUp

    Shared Sub UnhandledException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)

        Dim unhandledException As New UnhandledExceptionView

        unhandledException.uxExceptionDetails.Text = e.Exception.ToString
        unhandledException.ShowDialog()

        If unhandledException.uxRestart.Checked Then
            Application.Restart()
        Else
            Application.Exit()
        End If

    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'Entry point
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Shared Sub Main()

        'All unhandled exceptions are handled by the sub UnhandledException()
        'which displays a BEAST exception dialog and gives the user the chance
        'to copy the error to clipboard for pasting into email etc..
        AddHandler Application.ThreadException, AddressOf UnhandledException

        'Application framework not enabled
        My.User.InitializeWithWindowsUser()

        'Users must be authenticated
        If My.User.IsAuthenticated Then

            Dim securityPrincipal As Security.ISecurityPrincipal = Nothing
            Dim applicationController As ApplicationController = Nothing

            Try
                'Custom security principal for use throughout the session
                securityPrincipal = ServiceGateway.Instance.StartUserSession

                AppDomain.CurrentDomain.SetThreadPrincipal(securityPrincipal)
                Thread.CurrentPrincipal = securityPrincipal

                applicationController = applicationController.GetInstance
                applicationController.RegisterNavigator(New ApplicationNavigator)

                'If a user holds more than 1 role they will have to select
                'the role they want applied for use throughout the session
                If securityPrincipal.Roles.Count > 1 Then applicationController.NavigateTo("SelectRoleView")
                applicationController.NavigateTo("ShellView")

            Catch ex As Exceptions.InvalidUserNameException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Invalid User")
            Catch ex As Exceptions.UserDisabledException
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "User Disabled")
            Catch ex As System.Net.Sockets.SocketException
                MsgBox("The xxxxx Server is unavailable, contact the System Administrator.", MsgBoxStyle.Exclamation, "Server Unavailable")
            End Try

        Else
            MsgBox("User not authenticated, the application will terminate.", MsgBoxStyle.Exclamation, "Authentication")
        End If

        My.Settings.Save()
        Application.Exit()

    End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文