ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/TSControlPanel/Program.cs
Revision: 186
Committed: Mon Jul 16 15:00:20 2012 UTC (10 years, 10 months ago) by william
File size: 6311 byte(s)
Log Message:
+ major fix to logging (all logs write to their respective logs and also get written into a single log)

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