static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { Exception ex = (Exception)e.ExceptionObject; // write it to a log file StringBuilder sb = new StringBuilder(); sb.Append(string.Format("{0} at {1}\r\n", ex.GetType().FullName, DateTime.Now.ToString("MM/dd/yy hh:mm:ss"))); sb.Append(string.Format("Runtime {0} terminating\r\n", e.IsTerminating ? "true" : "false")); sb.Append(string.Format("Message: {0}\r\n", ex.Message)); sb.Append(string.Format("Stack trace: {0}\r\n", ex.StackTrace)); Trace2.WriteLine(sb.ToString()); if (Debugger.IsAttached) { Debugger.Break(); } else { string exceptionPath = Path.Combine(AppPath, EXCEPTION_FILE_NAME); StreamWriter writer = File.Exists(exceptionPath) ? File.AppendText(exceptionPath) : File.CreateText(exceptionPath); writer.Write(sb.ToString()); writer.Close(); } }
private void HandleException(Exception ex) { StringBuilder sb = new StringBuilder(); sb.Append(string.Format("{0} at {1}\r\n", ex.GetType().FullName, DateTime.Now.ToString("MM/dd/yy hh:mm:ss"))); sb.Append(string.Format("Message: {0}\r\n", ex.Message)); sb.Append(string.Format("Stack trace: {0}\r\n", ex.StackTrace)); Trace2.WriteLine(sb.ToString()); if (Debugger.IsAttached) { Debugger.Break(); } else { string exceptionPath = Path.Combine(Program.AppPath, "exceptionlog.txt"); StreamWriter writer = File.Exists(exceptionPath) ? File.AppendText(exceptionPath) : File.CreateText(exceptionPath); writer.Write(sb.ToString()); writer.Close(); } }