To catch any uncaught exception, I'm subscribing a Handler in the Main-Method()
like so:
Application.ThreadException += Application_ThreadException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
This works perfectly fine and catches ANY unhandled exception (EXCEPT StackOverflowException
), whether it's a UI-Thread or a Background-Thread Exception. (Only used for logging / Error Reporting, like this:)
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
//Log exception
Log.e("Unhandled Background Thread Exception", e.Exception);
//Show oops.
ExceptionHandler eh = new ExceptionHandler();
eh.Exception = e.Exception;
eh.ShowDialog();
eh.Dispose();
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Log exception
Log.e("Unhandled UI Thread Exception", (Exception)e.ExceptionObject);
//Show oops.
ExceptionHandler eh = new ExceptionHandler();
eh.Exception = (Exception)e.ExceptionObject;
eh.ShowDialog();
eh.Dispose();
}
However, to trace down a problem it usually helps to figure out, what the user was doing BEFORE the exception appeared... So, I was wondering if there is an "equal" way, to subscribe to any other event within the application?
i.e. something like a global event:
Application.EveryButtonClick += Application_ButtonClicked;
...
private static void Application_ButtonClicked(Object sender, Args e, Method calledMethod){
Log.Trace("User clicked on " + ((Button)sender).Name);
calledMethod.Invoke(sender, e);
}
Or something that's called an Interceptor
in Java-EE (Handled through Annotations)?
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
logger.debug("LoggingInterceptor - before EJB method invoke: "
+ ctx.getMethod().getName());
Object result = ctx.proceed(); //<-- Call initial method
logger.debug("LoggingInterceptor - after EJB method invoke: "
+ ctx.getMethod().getName() + " Call result was: " + result.toString());
return result;
}
Or something similar? There are quite some "events" I would like to track from a "global" point of view...
The example is quite "small" and buttons aren't that hard to handle.
But maybe I need a global "Listview_selected_index_changed(object sender, args e, Method invokedMethod )" handler?