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

Contents of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 98 - (show annotations) (download)
Sat Jul 14 03:14:54 2012 UTC (7 years, 7 months ago) by william
File size: 6008 byte(s)
+ more work

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 Logging.Initialize();
20 using (log4net.NDC.Push("Logged from ATSAdmin.exe"))
21 {
22 AppDomain currentDomain = AppDomain.CurrentDomain;
23 currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
24 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
25 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
26
27 Application.EnableVisualStyles();
28 Application.SetCompatibleTextRenderingDefault(false);
29 Application.Run(new frmAdmin());
30 }
31 }
32 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
33 {
34 CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
35 }
36 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
37 {
38 Logging.Initialize();
39 DialogResult result = DialogResult.Cancel;
40 Exception e = (Exception)args.ExceptionObject;
41 if (e == null)
42 {
43 Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null");
44 return;
45 }
46 if (e.GetType() == typeof(SqlException))
47 {
48 SqlException sql_ex = (e as SqlException);
49 List<string> strs = new List<string>();
50 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())))
51 {
52 Exception inner = sql_ex.InnerException;
53 if (inner != null)
54 {
55
56 if (inner.GetType() == typeof(SqlException))
57 {
58 SqlException sql_inner = inner as SqlException;
59 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())))
60 {
61 strs = Logging.GetMessagesFromThreadContextStack("NDC");
62 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
63 }
64 }
65 else
66 {
67 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString())))
68 {
69 strs = Logging.GetMessagesFromThreadContextStack("NDC");
70 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
71 }
72 }
73 }
74 else
75 {
76 strs = Logging.GetMessagesFromThreadContextStack("NDC");
77 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
78 }
79 }
80 StringBuilder builder = new StringBuilder();
81 foreach (string str in strs) { builder.AppendLine(str); }
82 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
83 if (result == DialogResult.Abort)
84 Application.Exit();
85 }
86 else
87 {
88 List<string> strs = new List<string>();
89 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
90 {
91 Exception inner = e.InnerException;
92 if (inner != null)
93 {
94 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString())))
95 {
96 strs = Logging.GetMessagesFromThreadContextStack("NDC");
97 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
98 }
99 }
100 else
101 {
102 strs = Logging.GetMessagesFromThreadContextStack("NDC");
103 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
104 }
105 }
106 StringBuilder builder = new StringBuilder();
107 foreach (string str in strs) { builder.AppendLine(str); }
108 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
109 if (result == DialogResult.Abort)
110 Application.Exit();
111 }
112 }
113 // Creates the error message and displays it.
114 private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message)
115 {
116 return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
117 }
118 }
119 }

  ViewVC Help
Powered by ViewVC 1.1.22