Oh - and any other thread you start ;-p
Any thread that exits because of an unhandled exception will break
your app; this is a good thing as it almost certainly indicates
instability. If you are using lots of threading, perhaps add your
error-logging method to AppDomain.UnhandledException (noting that
ExceptionObject will almost certainly be an Exception).
A very crude (and untested) implementation is shown below. Note that
you might need your installer to create the named ("MyService") event-
source, otherwise this could iteslf explode.
AppDomain.CurrentDomain.UnhandledException +=
CurrentDomain_UnhandledException;
....
static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e) {
Exception ex = e.ExceptionObject as Exception;
if (ex == null) {
string msg; // try to log whatever the heck happened!
try { msg = e.ExceptionObject.ToString(); }
catch { msg = "(unreadable exception)"; }
EventLog.WriteEntry("MyService", msg,
EventLogEntryType.Error);
} else {
// log outermost message and stack-trace
EventLog.WriteEntry("MyService", ex.Message,
EventLogEntryType.Error);
EventLog.WriteEntry("MyService", ex.StackTrace,
EventLogEntryType.Information);
while ((ex = ex.InnerException) != null) { // log
inner exception messages
EventLog.WriteEntry("MyService", ex.Message,
EventLogEntryType.Information);
}
}
}