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

Annotation of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (hide annotations) (download)
Thu Jul 12 19:40:36 2012 UTC (8 years, 11 months ago) by william
File size: 3881 byte(s)
+ show abort, retry, ignore dialog to user when unhandled exception is shown

1 william 4 using System;
2     using System.Collections.Generic;
3     using System.Windows.Forms;
4 william 52 using log4net;
5     using System.Data.SqlClient;
6 william 66 using System.Collections;
7     using System.Text;
8 william 4
9     namespace AnywhereTS
10     {
11     static class Program
12     {
13     /// <summary>
14     /// The main entry point for the application.
15     /// </summary>
16     [STAThread]
17     static void Main()
18 william 61 {
19     AppDomain currentDomain = AppDomain.CurrentDomain;
20     currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
21     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
22 william 65 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23 william 61
24 william 4 Application.EnableVisualStyles();
25     Application.SetCompatibleTextRenderingDefault(false);
26     Application.Run(new frmAdmin());
27     }
28 william 65 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
29     {
30     CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
31     }
32 william 61 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
33     {
34 william 68 DialogResult result = DialogResult.Cancel;
35 william 61 Exception e = (Exception)args.ExceptionObject;
36     if (e == null)
37     {
38     Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null");
39     return;
40     }
41     if (e.GetType() == typeof(SqlException))
42     {
43     SqlException sql_ex = (e as SqlException);
44 william 67 Stack stack = new Stack();
45 william 61 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())))
46     {
47 william 67 stack = log4net.NDC.CloneStack();
48 william 61 Logging.ATSAdminLog.Error("Encountered unhandled SqlException");
49     }
50 william 66 List<object> strs = new List<object>(stack.ToArray());
51     strs.Reverse();
52     StringBuilder builder = new StringBuilder();
53     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
54 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
55     if (result == DialogResult.Abort)
56     Application.Exit();
57 william 61 }
58     else
59     {
60 william 67 Stack stack = new Stack();
61 william 61 using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
62     {
63 william 67 stack = log4net.NDC.CloneStack();
64 william 61 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
65 william 67 }
66 william 66 List<object> strs = new List<object>(stack.ToArray());
67     strs.Reverse();
68     StringBuilder builder = new StringBuilder();
69     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
70 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
71     if (result == DialogResult.Abort)
72     Application.Exit();
73 william 61 }
74     }
75 william 68 // Creates the error message and displays it.
76     private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message)
77     {
78     return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
79     }
80 william 4 }
81     }

  ViewVC Help
Powered by ViewVC 1.1.22