You do not need to use Application.Exit or Environment.Exit, it is just the behavior of the visual studio that it continues to catch the string of unhandled exceptions, but in fact your application will crash (exit) if the flag isTerminating and this is its correct use. I would recommend not using any user interface for this or any other resource-intensive process in this case.
the purpose of this event is to get a log of why this happened this way, this does not mean that your application looks good while it crashes, or apologizing for it. but you will have the opportunity to do it higher the next time the user logs in.
Therefore, even if you plan to send an error log, the best way is to write the file and send the file by e-mail the next time you start the application.
I usually use log4net and use these events as follows.
[STAThread] static void Main() { log.Info("Starting deployer application"); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.ApplicationExit += new EventHandler(Application_ApplicationExit); Application.Run(ConnectionForm.GetInstance); } static void Application_ApplicationExit(object sender, EventArgs e) { log.Info("Application Closed"); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { log.Error(string.Format("*** UNHANDLED APPDOMAIN EXCEPTION ({0}) *****", e.IsTerminating ? "Terminating" : "Non-Terminating"), e.ExceptionObject as Exception); } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { log.Error("*** UNHANDLED THREAD EXCEPTION *****", e.Exception); }
If you have never used log4net before this may be useful .
source share