/[AnywhereTS-MSSQL]/trunk/TSControlPanel/Program.cs
ViewVC logotype

Diff of /trunk/TSControlPanel/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/TSControlPanel/Program.cs	2012/07/12 16:30:29	53
+++ trunk/TSControlPanel/Program.cs	2012/07/12 19:23:51	66
@@ -2,6 +2,8 @@
 using System.Collections.Generic;
 using System.Windows.Forms;
 using System.Data.SqlClient;
+using System.Collections;
+using System.Text;
 
 namespace AnywhereTS
 {
@@ -13,7 +15,12 @@
         [STAThread]
         static void Main()
         {
-            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
+            AppDomain currentDomain = AppDomain.CurrentDomain;
+            currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
+            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
+
+            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
+
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
 
@@ -21,28 +28,47 @@
             objCustomDialogBox.dialogMode = frmClientProperties.ATSClientMode.CONTROL_PANEL; // Select the mode to run the form in.
             Application.Run(objCustomDialogBox);            
         }
-        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
+
+        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
+        {
+            CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
+        }
+        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
         {
-            Exception ex = (e.ExceptionObject as Exception);
-            if (ex == null)
+            Exception e = (Exception)args.ExceptionObject;
+            if (e == null)
             {
                 Logging.TSControlPanelLog.Error("Encountered unhandled Exception, but the exception was null");
                 return;
             }
-            if (e.ExceptionObject.GetType() == typeof(SqlException))
+            if (e.GetType() == typeof(SqlException))
             {
-                SqlException sql_ex = (ex as SqlException);
+                SqlException sql_ex = (e as SqlException);
                 using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
                 {
                     Logging.TSControlPanelLog.Error("Encountered unhandled SqlException");
                 }
+                Stack stack = log4net.NDC.CloneStack();
+                List<object> strs = new List<object>(stack.ToArray());
+                strs.Reverse();
+                StringBuilder builder = new StringBuilder();
+                foreach (object str in strs) { builder.AppendLine(str.ToString()); }
+                builder.AppendLine("Encountered unhandled Exception");
+                MessageBox.Show(builder.ToString());
             }
             else
             {
-                using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", ex.Message, System.Environment.NewLine, ex.ToString())))
+                using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
                 {
                     Logging.TSControlPanelLog.Error("Encountered unhandled Exception");
                 }
+                Stack stack = log4net.NDC.CloneStack();
+                List<object> strs = new List<object>(stack.ToArray());
+                strs.Reverse();
+                StringBuilder builder = new StringBuilder();
+                foreach (object str in strs) { builder.AppendLine(str.ToString()); }
+                builder.AppendLine("Encountered unhandled Exception");
+                MessageBox.Show(builder.ToString());
             }
         }
     }

 

  ViewVC Help
Powered by ViewVC 1.1.22