ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/AnywhereTS-MSSQL/trunk/TSAdminTool/Program.cs
Revision: 70
Committed: Thu Jul 12 20:47:35 2012 UTC (11 years, 4 months ago) by william
File size: 3728 byte(s)
Log Message:
+ get NDC ThreadContectStack using reflection

File Contents

# Content
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 DialogResult result = DialogResult.Cancel;
35 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 List<string> strs = new List<string>();
45 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 {
47 strs = Logging.GetMessagesFromThreadContextStack("NDC");
48 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
49 }
50 StringBuilder builder = new StringBuilder();
51 foreach (string str in strs) { builder.AppendLine(str); }
52 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
53 if (result == DialogResult.Abort)
54 Application.Exit();
55 }
56 else
57 {
58 List<string> strs = new List<string>();
59 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString())))
60 {
61 strs = Logging.GetMessagesFromThreadContextStack("NDC");
62 Logging.ATSAdminLog.Error("Encountered unhandled Exception");
63 }
64 StringBuilder builder = new StringBuilder();
65 foreach (string str in strs) { builder.AppendLine(str); }
66 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
67 if (result == DialogResult.Abort)
68 Application.Exit();
69 }
70 }
71 // 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 }
77 }