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

Contents of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

1 using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using log4net;
5 using System.Data.SqlClient;
6 using System.Collections;
7 using System.Text;
8
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 {
19 AppDomain currentDomain = AppDomain.CurrentDomain;
20 currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
21 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
22 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23
24 Application.EnableVisualStyles();
25 Application.SetCompatibleTextRenderingDefault(false);
26 Application.Run(new frmAdmin());
27 }
28 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
29 {
30 CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
31 }
32 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
33 {
34 DialogResult result = DialogResult.Cancel;
35 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 List<string> strs = new List<string>();
45 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 {
47 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 }
75 StringBuilder builder = new StringBuilder();
76 foreach (string str in strs) { builder.AppendLine(str); }
77 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
78 if (result == DialogResult.Abort)
79 Application.Exit();
80 }
81 else
82 {
83 List<string> strs = new List<string>();
84 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
85 {
86 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 }
101 StringBuilder builder = new StringBuilder();
102 foreach (string str in strs) { builder.AppendLine(str); }
103 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
104 if (result == DialogResult.Abort)
105 Application.Exit();
106 }
107 }
108 // 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 }
114 }

  ViewVC Help
Powered by ViewVC 1.1.22