WINFORM automatic exception capture

Source: Internet
Author: User
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 );}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.