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

Annotation of /trunk/TSControlPanel/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 66 - (hide annotations) (download)
Thu Jul 12 19:23:51 2012 UTC (8 years, 11 months ago) by william
File size: 3501 byte(s)
+ add support in unhandled exception handler to display the log4net output in a messagebox

1 william 4 using System;
2     using System.Collections.Generic;
3     using System.Windows.Forms;
4 william 52 using System.Data.SqlClient;
5 william 66 using System.Collections;
6     using System.Text;
7 william 4
8     namespace AnywhereTS
9     {
10     static class Program
11     {
12     /// <summary>
13     /// The main entry point for the application.
14     /// </summary>
15     [STAThread]
16     static void Main()
17 william 61 {
18     AppDomain currentDomain = AppDomain.CurrentDomain;
19     currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
20     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
21    
22 william 65 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23    
24 william 4 Application.EnableVisualStyles();
25     Application.SetCompatibleTextRenderingDefault(false);
26    
27     frmClientProperties objCustomDialogBox = new frmClientProperties();
28     objCustomDialogBox.dialogMode = frmClientProperties.ATSClientMode.CONTROL_PANEL; // Select the mode to run the form in.
29     Application.Run(objCustomDialogBox);
30     }
31 william 65
32     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     Exception e = (Exception)args.ExceptionObject;
39     if (e == null)
40     {
41 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled Exception, but the exception was null");
42 william 61 return;
43     }
44     if (e.GetType() == typeof(SqlException))
45     {
46     SqlException sql_ex = (e as SqlException);
47     using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
48     {
49 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled SqlException");
50 william 61 }
51 william 66 Stack stack = log4net.NDC.CloneStack();
52     List<object> strs = new List<object>(stack.ToArray());
53     strs.Reverse();
54     StringBuilder builder = new StringBuilder();
55     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
56     builder.AppendLine("Encountered unhandled Exception");
57     MessageBox.Show(builder.ToString());
58 william 61 }
59     else
60     {
61     using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
62     {
63 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled Exception");
64 william 61 }
65 william 66 Stack stack = log4net.NDC.CloneStack();
66     List<object> strs = new List<object>(stack.ToArray());
67     strs.Reverse();
68     StringBuilder builder = new StringBuilder();
69     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
70     builder.AppendLine("Encountered unhandled Exception");
71     MessageBox.Show(builder.ToString());
72 william 61 }
73     }
74 william 4 }
75     }

  ViewVC Help
Powered by ViewVC 1.1.22