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

Annotation of /trunk/TSAdminTool/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 70 - (hide annotations) (download)
Thu Jul 12 20:47:35 2012 UTC (9 years, 2 months ago) by william
File size: 3728 byte(s)
+ get NDC ThreadContectStack using reflection

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 william 68 DialogResult result = DialogResult.Cancel;
35 william 61 Exception e = (Exception)args.ExceptionObject;
36     if (e == null)
37     {
38     Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null");
39     return;
40     }
41     if (e.GetType() == typeof(SqlException))
42     {
43     SqlException sql_ex = (e as SqlException);
44 william 70 List<string> strs = new List<string>();
45 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())))
46 william 61 {
47 william 70 strs = Logging.GetMessagesFromThreadContextStack("NDC");
48 william 69 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
49 william 61 }
50 william 66 StringBuilder builder = new StringBuilder();
51 william 70 foreach (string str in strs) { builder.AppendLine(str); }
52 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
53     if (result == DialogResult.Abort)
54     Application.Exit();
55 william 61 }
56     else
57     {
58 william 70 List<string> strs = new List<string>();
59 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())))
60 william 61 {
61 william 70 strs = Logging.GetMessagesFromThreadContextStack("NDC");
62 william 61 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
63 william 69 }
64 william 66 StringBuilder builder = new StringBuilder();
65 william 70 foreach (string str in strs) { builder.AppendLine(str); }
66 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
67     if (result == DialogResult.Abort)
68     Application.Exit();
69 william 61 }
70     }
71 william 68 // Creates the error message and displays it.
72     private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message)
73     {
74     return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
75     }
76 william 4 }
77     }

  ViewVC Help
Powered by ViewVC 1.1.22