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

Annotation of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (hide annotations) (download)
Thu Jul 12 22:00:37 2012 UTC (8 years, 11 months ago) by william
File size: 5814 byte(s)
+ attempt to get inner exception, if any

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 70 List<string> strs = new List<string>();
45 william 69 using (log4net.NDC.Push(string.Format("{0}: ID={1} MESSAGE={2}{3}Diagnostics:{3}{4}", sql_ex.GetType().Name, sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
46 william 61 {
47 william 73 Exception inner = sql_ex.InnerException;
48     if (inner != null)
49     {
50    
51     if (inner.GetType() == typeof(SqlException))
52     {
53     SqlException sql_inner = inner as SqlException;
54     using (log4net.NDC.Push(string.Format("{0}: ID={1} MESSAGE={2}{3}Diagnostics:{3}{4}", sql_inner.GetType().Name, sql_inner.Number.ToString(), sql_inner.Message, System.Environment.NewLine, sql_inner.ToString())))
55     {
56     strs = Logging.GetMessagesFromThreadContextStack("NDC");
57     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
58     }
59     }
60     else
61     {
62     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString())))
63     {
64     strs = Logging.GetMessagesFromThreadContextStack("NDC");
65     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
66     }
67     }
68     }
69     else
70     {
71     strs = Logging.GetMessagesFromThreadContextStack("NDC");
72     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
73     }
74 william 61 }
75 william 66 StringBuilder builder = new StringBuilder();
76 william 70 foreach (string str in strs) { builder.AppendLine(str); }
77 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
78     if (result == DialogResult.Abort)
79     Application.Exit();
80 william 61 }
81     else
82     {
83 william 70 List<string> strs = new List<string>();
84 william 69 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
85 william 61 {
86 william 73 Exception inner = e.InnerException;
87     if (inner != null)
88     {
89     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString())))
90     {
91     strs = Logging.GetMessagesFromThreadContextStack("NDC");
92     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
93     }
94     }
95     else
96     {
97     strs = Logging.GetMessagesFromThreadContextStack("NDC");
98     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
99     }
100 william 69 }
101 william 66 StringBuilder builder = new StringBuilder();
102 william 70 foreach (string str in strs) { builder.AppendLine(str); }
103 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
104     if (result == DialogResult.Abort)
105     Application.Exit();
106 william 61 }
107     }
108 william 68 // Creates the error message and displays it.
109     private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message)
110     {
111     return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
112     }
113 william 4 }
114     }

  ViewVC Help
Powered by ViewVC 1.1.22