views:

56

answers:

4

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?

A: 

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
David
A: 

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

http://www.4guysfromrolla.com/articles/031407-1.aspx

Joel Etherton
Thanks, but our app is not ASP.Net, only .Net. We do not even have a Web.config where this should be configured
shojtsy
@shotjsy: Perhaps something like this then? http://www.codeproject.com/KB/exception/UnhandledExceptionClass.aspx
Joel Etherton
+2  A: 

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

0xA3
A: 

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

Paul Betts