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?
Open the Application Insights Search telemetry window in Visual Studio. While debugging, select the Application Insights dropdown box. Select an exception report to show its stack trace.
If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application.” This can be helpful if you can't figure out why your application suddenly crashes. Windows Event Viewer may log two different entries for the same exception.
An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist. The . NET Framework will then throw a FileNotFoundException.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With