ea6c2facb20f88655274c48c582546c52a2b68bf.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.Text;
  5. using System.IO;
  6. namespace Core.Mes.ServerManager
  7. {
  8. static class Program
  9. {
  10. /// <summary>
  11. /// 应用程序的主入口点。
  12. /// </summary>
  13. [STAThread]
  14. static void Main()
  15. {
  16. try
  17. {
  18. CreateLogDirectory();
  19. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  20. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  21. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  22. Application.EnableVisualStyles();
  23. Application.SetCompatibleTextRenderingDefault(false);
  24. FrmSeverMain frm = new FrmSeverMain();
  25. ExitMessageFilter msg = new ExitMessageFilter(frm);
  26. Application.AddMessageFilter(msg);
  27. Application.Run(frm);
  28. }
  29. catch (Exception ex)
  30. {
  31. string path = string.Format(@"{0}/log/Main/Main_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
  32. StringBuilder sbtxt = new StringBuilder();
  33. GetException(ex, sbtxt);
  34. File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);
  35. }
  36. }
  37. private static void CreateLogDirectory()
  38. {
  39. string path = string.Format(@"{0}/log/Main/", Application.StartupPath);
  40. if (!Directory.Exists(path))
  41. {
  42. Directory.CreateDirectory(path);
  43. }
  44. path = string.Format(@"{0}/log/Application/", Application.StartupPath);
  45. if (!Directory.Exists(path))
  46. {
  47. Directory.CreateDirectory(path);
  48. }
  49. path = string.Format(@"{0}/log/Method/", Application.StartupPath);
  50. if (!Directory.Exists(path))
  51. {
  52. Directory.CreateDirectory(path);
  53. }
  54. }
  55. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  56. {
  57. Exception ex = e.ExceptionObject as Exception;
  58. string path = string.Format(@"{0}/log/Main/Main_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
  59. StringBuilder sbtxt = new StringBuilder();
  60. GetException(ex, sbtxt);
  61. File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);
  62. }
  63. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  64. {
  65. string path = string.Format(@"{0}/log/Main/ThreadException_{1}.txt", Application.StartupPath, System.DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss"));
  66. StringBuilder sbtxt = new StringBuilder();
  67. GetException(e.Exception, sbtxt);
  68. File.WriteAllText(path, sbtxt.ToString(), Encoding.UTF8);
  69. }
  70. public static void GetException(Exception ex, StringBuilder sbtxt)
  71. {
  72. if (ex.InnerException != null)
  73. {
  74. GetException(ex.InnerException, sbtxt);
  75. }
  76. else
  77. {
  78. sbtxt.AppendLine("==============================================");
  79. sbtxt.AppendLine(ex.Message);
  80. sbtxt.AppendLine("==============================================");
  81. sbtxt.AppendLine(ex.StackTrace);
  82. sbtxt.AppendLine("==============================================");
  83. }
  84. }
  85. }
  86. }