471,853 Members | 824 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Global exception handler for windows service application

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?
Nov 20 '05 #1
7 6690
The AppDomain class has an UnhandledException event. Try to handle that and
put the logging code in there.

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?

Nov 20 '05 #2
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?

"Marina" <so*****@nospam.com> wrote in message
news:eK**************@TK2MSFTNGP10.phx.gbl...
The AppDomain class has an UnhandledException event. Try to handle that and put the logging code in there.

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?


Nov 20 '05 #3
Check out the WriteEntry method to write to the event log.

http://msdn.microsoft.com/library/de...classtopic.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.
Nov 20 '05 #4
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" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Check out the WriteEntry method to write to the event log.

http://msdn.microsoft.com/library/de...classtopic.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.
Nov 20 '05 #5
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?

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?

Nov 20 '05 #6
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.

"Adam J. Schaff" <as*****@cascocdev.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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?

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Claudio Di Flumeri | last post: by
2 posts views Thread by yeghia \(sosy\) | last post: by
6 posts views Thread by Steve | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.