Using System; using System. collections. generic; using System. linq; using System. windows. forms; namespace WinformLogDemo {static class Program {/// <summary> /// main entry point of the application. /// </Summary> [STAThread] static void Main () {try {Application. enableVisualStyles (); Application. setCompatibleTextRenderingDefault (false); Application. threadException + = new System. threading. threadExceptionEventHandler (Application_ThreadException); AppDomain. currentDomain. unhandledException + = new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException); Application. run (new Form1 ());} Catch (Exception ex) {MessageBox. show (ex. message, "system error", MessageBoxButtons. OK, MessageBoxIcon. error) ;}} static void Application_ThreadException (object sender, System. threading. threadExceptionEventArgs e) {string str = ""; Exception error = e. exception as Exception; if (error! = Null) {str = string. format ("exception type: {0} \ r \ n exception message: {1} \ r \ n exception information: {2} \ r \ n", error. getType (). name, error. message, error. stackTrace);} else {str = string. format ("Application Thread error: {0}", e) ;}messagebox. show ("a fatal error occurs. Please contact the author in time! "+ Str," 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) ;}messagebox. show ("a fatal error occurs. Please stop the current operation and contact the author in time! "," System Error ", MessageBoxButtons. OK, MessageBoxIcon. Error );}}}