ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/TSControlPanel/Program.cs
Revision: 96
Committed: Sat Jul 14 03:01:07 2012 UTC (11 years, 5 months ago) by william
File size: 6220 byte(s)
Log Message:
force initialize logging

File Contents

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