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

5
我正在开发一个.NET服务器应用程序,该应用程序应该连续运行。每次服务器进程因任何原因终止时,我希望发送一封通知电子邮件。理想情况下,如果异常导致终止,电子邮件应包含异常消息和堆栈跟踪。我知道某些异常无法捕获,例如StackOverflowException。那么如何记录/发送通知表明服务器进程中发生了StackOverflowException
我考虑创建第二个进程来监视服务器进程。但是,它如何获取发生的异常详细信息?
4个回答

4
致命的异常,例如StackOverflowException通常会在Windows事件日志中记录(甚至可能包括堆栈跟踪)。您可以监视事件日志(例如使用另一个服务),该服务将在特定条目上发送电子邮件。这里描述了一个用于监视事件日志条目的简单应用程序: 实时事件日志监视工具 和这里: 当错误写入事件日志时发送电子邮件警报 如果您希望您的服务持续运行,您可能希望配置它在崩溃时自动重启(尽管您应该知道通常是它崩溃的原因,并且仅仅重新启动并不一定会消除那个原因)。您可以在服务属性的“恢复”选项卡下这样做。在那里,您还可以选择在服务崩溃时调用自定义程序或脚本。

使用内置的Windows功能的另一个选项是配置事件日志以发送电子邮件通知。 这至少适用于从Windows Vista开始,如此处所述:

如何在Vista中设置事件查看器以发送电子邮件通知


1
实际上,您根本不需要任何第三方软件来完成这个任务 - Win7任务计划程序可以在看到特定类型的事件并且未处理的异常被写入事件日志时触发预定任务。内置操作之一是“发送电子邮件”,因此您可以在操作系统中得到整个解决方案。

0

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

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

0

谢谢,但我们的应用程序不是ASP.Net,只是.Net。我们甚至没有Web.config文件来配置这个。 - shojtsy
@shotjsy:也许可以试试这个?http://www.codeproject.com/KB/exception/UnhandledExceptionClass.aspx - Joel Etherton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接