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

Annotation of /trunk/TSAdminTool/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: 3261 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 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     AppDomain currentDomain = AppDomain.CurrentDomain;
20     currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
21     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
22 william 65 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23 william 61
24 william 4 Application.EnableVisualStyles();
25     Application.SetCompatibleTextRenderingDefault(false);
26     Application.Run(new frmAdmin());
27     }
28 william 65 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
29     {
30     CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
31     }
32 william 61 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
33     {
34     Exception e = (Exception)args.ExceptionObject;
35     if (e == null)
36     {
37     Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null");
38     return;
39     }
40     if (e.GetType() == typeof(SqlException))
41     {
42     SqlException sql_ex = (e as SqlException);
43     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())))
44     {
45     Logging.ATSAdminLog.Error("Encountered unhandled SqlException");
46     }
47 william 66 Stack stack = log4net.NDC.CloneStack();
48     List<object> strs = new List<object>(stack.ToArray());
49     strs.Reverse();
50     StringBuilder builder = new StringBuilder();
51     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
52     builder.AppendLine("Encountered unhandled Exception");
53     MessageBox.Show(builder.ToString());
54 william 61 }
55     else
56     {
57     using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
58     {
59     Logging.ATSAdminLog.Error("Encountered unhandled Exception");
60     }
61 william 66 Stack stack = log4net.NDC.CloneStack();
62     List<object> strs = new List<object>(stack.ToArray());
63     strs.Reverse();
64     StringBuilder builder = new StringBuilder();
65     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
66     builder.AppendLine("Encountered unhandled Exception");
67     MessageBox.Show(builder.ToString());
68 william 61 }
69     }
70 william 4 }
71     }

  ViewVC Help
Powered by ViewVC 1.1.22