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

Annotation of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 94 - (hide annotations) (download)
Sat Jul 14 02:52:23 2012 UTC (8 years, 11 months ago) by william
File size: 5945 byte(s)
+ add lots of debug logging with proper context

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

  ViewVC Help
Powered by ViewVC 1.1.22