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

Annotation of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (hide annotations) (download)
Mon Jul 16 15:00:20 2012 UTC (8 years, 2 months ago) by william
File size: 6038 byte(s)
+ major fix to logging (all logs write to their respective logs and also get written into a single log)

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 186 Logging.UpdateLogPath();
20 william 97 using (log4net.NDC.Push("Logged from ATSAdmin.exe"))
21 william 94 {
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 william 61
27 william 94 Application.EnableVisualStyles();
28     Application.SetCompatibleTextRenderingDefault(false);
29     Application.Run(new frmAdmin());
30     }
31 william 4 }
32 william 65 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
33     {
34     CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
35     }
36 william 61 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
37     {
38 william 68 DialogResult result = DialogResult.Cancel;
39 william 61 Exception e = (Exception)args.ExceptionObject;
40     if (e == null)
41     {
42     Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null");
43     return;
44     }
45     if (e.GetType() == typeof(SqlException))
46     {
47 william 128 SqlException sql_ex = (e.GetBaseException() as SqlException);
48 william 70 List<string> strs = new List<string>();
49 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())))
50 william 61 {
51 william 73 Exception inner = sql_ex.InnerException;
52     if (inner != null)
53     {
54    
55     if (inner.GetType() == typeof(SqlException))
56     {
57     SqlException sql_inner = inner as SqlException;
58     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())))
59     {
60     strs = Logging.GetMessagesFromThreadContextStack("NDC");
61     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
62     }
63     }
64     else
65     {
66     using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString())))
67     {
68     strs = Logging.GetMessagesFromThreadContextStack("NDC");
69     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
70     }
71     }
72     }
73     else
74     {
75     strs = Logging.GetMessagesFromThreadContextStack("NDC");
76     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
77     }
78 william 61 }
79 william 66 StringBuilder builder = new StringBuilder();
80 william 70 foreach (string str in strs) { builder.AppendLine(str); }
81 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
82     if (result == DialogResult.Abort)
83     Application.Exit();
84 william 61 }
85     else
86     {
87 william 128 e = e.GetBaseException();
88 william 70 List<string> strs = new List<string>();
89 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())))
90 william 61 {
91 william 73 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 william 69 }
106 william 66 StringBuilder builder = new StringBuilder();
107 william 70 foreach (string str in strs) { builder.AppendLine(str); }
108 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
109     if (result == DialogResult.Abort)
110     Application.Exit();
111 william 61 }
112     }
113 william 68 // 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 william 4 }
119     }

  ViewVC Help
Powered by ViewVC 1.1.22