Winform-global exception capture function, winform-Global Capture
Using System; using System. collections. generic; using System. linq; using System. windows. forms; using JxsFL. caiGou; using JxsFL. cangKu; using JxsFL. cangKu. out; using JxsFL. KJ; using JxsFL. LEDs; using DevExpress. lookAndFeel; using System. IO; namespace JxsFL {static class Program {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {try {// process uncaptured exception Application. setUnhandledExceptionMode (UnhandledExceptionMode. catchException); // handle the UI thread exception Application. threadException + = new System. threading. threadExceptionEventHandler (Application_ThreadException); // handle non-UI thread exception AppDomain. currentDomain. unhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); Applicat Ion. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); if (System. diagnostics. process. getProcessesByName (System. diagnostics. process. getCurrentProcess (). processName ). length> 1) {MessageBox. show ("one already runs");} else {DevExpress. userSkins. bonusSkins. register (); DevExpress. userSkins. officeSkins. register (); DevExpress. skins. skinManager. enableFormSkins (); UserLookAndFeel. defau Lt. setSkinStyle ("Office 2010 Blue"); Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); // Application. run (new JxsFL. mead. shiFuDCL. shiFuDCLFind (); Application. run (new Login () ;}} catch (Exception ex) {string str = ""; string strDateInfo = "Exceptions not handled by the application:" + DateTime. now. toString () + "\ r \ n"; if (ex! = Null) {str = string. format (strDateInfo + "exception type: {0} \ r \ n exception message: {1} \ r \ n exception message: {2} \ r \ n", ex. getType (). name, ex. message, ex. stackTrace);} else {str = string. format ("Application Thread error: {0}", ex) ;}writelog (str); MessageBox. show ("a fatal error occurred. Please contact Shenzhen jinjiano software technical support in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error); }}/// <summary> /// write an exception to the text file, and a prompt box is displayed (custom, such as uploading to an email) /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> static void Application_ThreadException (object sender, system. threading. threadExceptionEventArgs e) {string str = ""; string strDateInfo = "Exceptions not handled by the application:" + DateTime. now. toString () + "\ r \ n"; Exception error = e. ex Ception as Exception; if (error! = Null) {str = string. format (strDateInfo + "exception type: {0} \ r \ n exception message: {1} \ r \ n exception message: {2} \ r \ n", error. getType (). name, error. message, error. stackTrace);} else {str = string. format ("Application Thread error: {0}", e) ;}writelog (str); MessageBox. show ("a fatal error occurs. Please contact the author in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error);} static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e) {string str = ""; Exception error = e. exceptionObject as Exception; string strDateInfo = "Exceptions not handled by the application:" + DateTime. now. toString () + "\ r \ n"; if (error! = Null) {str = string. format (strDateInfo + "Application UnhandledException: {0}; \ n \ r stack information: {1}", error. message, error. stackTrace);} else {str = string. format ("Application UnhandledError: {0}", e) ;}writelog (str); MessageBox. show ("a fatal error occurs. Please stop the current operation and contact the author in time! "," System error ", MessageBoxButtons. OK, MessageBoxIcon. error );} /// <summary> /// write a file /// </summary> /// <param name = "str"> </param> static void writeLog (string str) {if (! Directory. exists ("ErrLog") {Directory. createDirectory ("ErrLog");} using (StreamWriter sw = new StreamWriter (@ "ErrLog \ ErrLog.txt", true) {sw. writeLine (str); sw. writeLine ("---------------------------------------------------------"); sw. close ();}}}}