You asked the wrong question, my friend!
Most frameworks allow exception handling with a specific method. WPF, C # webforms, asp, they all have a raw exception handling routine that you can connect to at the application level.
For example, regular applications with C # formats use:
Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod) private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t) {
Thus, you just need to declare your class as exceptionmanager, and then include the class in exception handling, for example:
Application.ThreadException += new ThreadExceptionEventHandler(TellMyClass) private static void TellMyClass(object sender, ThreadExceptionEventArgs t) { ExceptionManager.HandleException(sender, t); }
However, the pattern I used is:
public static class UnhandledExceptionManager { Logger _logger; public static void RegisterToHandleFormsException(){ _logger = new Logger(); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += OnThreadException; AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; } public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e){ HandleException((Exception)e.ExceptionObject); } private static void HandleException(Exception exception, [CallerMemberName] string methodName = "") { try { _logger.Error(methodName, exception); } catch (Exception e) { Debug.WriteLine("({0}) {1}", methodName, e); } } }
What is used in Program.cs:
public static void Main(){ UnhandledExceptionManager.RegisterToHandleFormsException();
C bauer
source share