using System; using System.Collections.Generic; using System.Windows.Forms; using System.Data.SqlClient; using System.Collections; using System.Text; using log4net; namespace AnywhereTS { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Logging.UpdateLogPath(); using (log4net.NDC.Push("Logged from TSControlPanel.exe")) { Logging.TSControlPanelLog.Debug("TSControlPanel.exe starting up"); AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); frmClientProperties objCustomDialogBox = new frmClientProperties(); objCustomDialogBox.dialogMode = frmClientProperties.ATSClientMode.CONTROL_PANEL; // Select the mode to run the form in. Application.Run(objCustomDialogBox); } } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false)); } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) { DialogResult result = DialogResult.Cancel; Exception e = (Exception)args.ExceptionObject; if (e == null) { Logging.ATSAdminLog.Error("Encountered unhandled Exception, but the exception was null"); return; } if (e.GetType() == typeof(SqlException)) { SqlException sql_ex = (e as SqlException); List strs = new List(); 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()))) { Exception inner = sql_ex.InnerException; if (inner != null) { if (inner.GetType() == typeof(SqlException)) { SqlException sql_inner = inner as SqlException; using (log4net.NDC.Push(string.Format("{0}: ID={1} MESSAGE={2}{3}Diagnostics:{3}{4}", sql_inner.GetType().Name, sql_inner.Number.ToString(), sql_inner.Message, System.Environment.NewLine, sql_inner.ToString()))) { strs = Logging.GetMessagesFromThreadContextStack("NDC"); Logging.ATSAdminLog.Error("Encountered unhandled Exception"); } } else { using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString()))) { strs = Logging.GetMessagesFromThreadContextStack("NDC"); Logging.ATSAdminLog.Error("Encountered unhandled Exception"); } } } else { strs = Logging.GetMessagesFromThreadContextStack("NDC"); Logging.ATSAdminLog.Error("Encountered unhandled Exception"); } } StringBuilder builder = new StringBuilder(); foreach (string str in strs) { builder.AppendLine(str); } result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString()); if (result == DialogResult.Abort) Application.Exit(); } else { List strs = new List(); using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", e.GetType().Name, e.Message, System.Environment.NewLine, e.ToString()))) { Exception inner = e.InnerException; if (inner != null) { using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", inner.GetType().Name, inner.Message, System.Environment.NewLine, inner.ToString()))) { strs = Logging.GetMessagesFromThreadContextStack("NDC"); Logging.ATSAdminLog.Error("Encountered unhandled Exception"); } } else { strs = Logging.GetMessagesFromThreadContextStack("NDC"); Logging.ATSAdminLog.Error("Encountered unhandled Exception"); } } StringBuilder builder = new StringBuilder(); foreach (string str in strs) { builder.AppendLine(str); } result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString()); if (result == DialogResult.Abort) Application.Exit(); } } // Creates the error message and displays it. private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message) { return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1); } } }