Global exception handler for windows service application

  • Thread starter Thread starter Adam J. Schaff
  • Start date Start date
A

Adam J. Schaff

I would like my windows service application to log all exceptions that
occur, on any of its threads, to the windows event log. Is this possible? If
so, can anyone give me an example of the necessary code?
 
The AppDomain class has an UnhandledException event. Try to handle that and
put the logging code in there.
 
I have added a handler for AppDomain.CurrentDomain.UnhandledException to
both the Shared Sub Main and the OnStart method, with no luck. In both
cases, I threw an exception immediately after adding the handler, but my
handler procedure never gets called.

Anyone have any other ideas?
 
Check out the WriteEntry method to write to the event log.

http://msdn.microsoft.com/library/d.../frlrfsystemdiagnosticseventlogclasstopic.asp

writing all exceptions that occur to the event log is not necessarily the best route. (you can if thats what you want)

log application (service) start, end, major warnings and important messages etc. in the event log
i think you should log most other exceptions and/or log messages to a user configurable logfile, since they might tend to grow larger.
 
Thanks for your input. I already know how to write to the event log. The
piece of the puzzle that eludes me is how do I intercept unhandled
exceptions in a windows service since they seem to just kill the thread (but
not the service) without my ever having a chance to handle them.

In other words, I can't find anywhere to place the WriteEntry code that will
get called when an exception occurs. :(

tMan said:
Check out the WriteEntry method to write to the event log.

http://msdn.microsoft.com/library/d.../frlrfsystemdiagnosticseventlogclasstopic.asp

writing all exceptions that occur to the event log is not necessarily the
best route. (you can if thats what you want)
log application (service) start, end, major warnings and important messages etc. in the event log
i think you should log most other exceptions and/or log messages to a user
configurable logfile, since they might tend to grow larger.
 
On further investigation, it's only exceptions occurring in threads other
than the main one that are not getting caught by my unhandled exception
handler. Does anyone know how I can get at those exceptions?
 
My problem is more mundane than I realized. I'm subclassing a class in a 3rd
party component and I now believe they are handling the exceptions from my
subclass and throwing them away before the appdomain unhandledexception
event is triggered. It looks like it's good ol' try..catch for me in the
subclass.
 
Back
Top