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

Contents of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

1 using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using log4net;
5 using System.Data.SqlClient;
6 using System.Collections;
7 using System.Text;
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 AppDomain currentDomain = AppDomain.CurrentDomain;
20 currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
21 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
22 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23
24 Application.EnableVisualStyles();
25 Application.SetCompatibleTextRenderingDefault(false);
26 Application.Run(new frmAdmin());
27 }
28 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
29 {
30 CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
31 }
32 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 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 }
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 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 }
69 }
70 }
71 }

  ViewVC Help
Powered by ViewVC 1.1.22