CatchUnhandledException.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Wayne.Lib
  7. {
  8. /// <summary>
  9. /// General handling of the application level unhandled exceptions.
  10. /// </summary>
  11. public static class ExceptionHandling
  12. {
  13. /// <summary>
  14. /// Create and register eventhandlers.
  15. /// </summary>
  16. public static void CatchUnhandledException()
  17. {
  18. AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
  19. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  20. }
  21. /// <summary>
  22. /// The eventhandler for Exceptions in the current domain.
  23. /// </summary>
  24. /// <param name="sender"></param>
  25. /// <param name="e"></param>
  26. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  27. {
  28. UnhandledException(e.ExceptionObject);
  29. }
  30. /// <summary>
  31. /// Handling a unhandled exception by creating a log.
  32. /// </summary>
  33. /// <param name="oException"></param>
  34. public static void UnhandledException(object oException)
  35. {
  36. try
  37. {
  38. try
  39. {
  40. LogCrashError(oException);
  41. }
  42. catch
  43. {
  44. }
  45. }
  46. finally
  47. {
  48. var thisProcess = Process.GetCurrentProcess();
  49. thisProcess.Kill();
  50. }
  51. }
  52. /// <summary>
  53. /// Writes the exception to the logfile.
  54. /// </summary>
  55. /// <param name="oException"></param>
  56. private static void LogCrashError(object oException)
  57. {
  58. using (var sw = new StreamWriter(string.Format(@"\Flash\Log\CrashLog_{0}.txt",
  59. DateTime.Today.ToString("yyyyMMdd")), true))
  60. {
  61. sw.WriteLine("**********************************************************************");
  62. sw.WriteLine(string.Format("* {0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
  63. sw.Write(GetExceptionInfo(oException as Exception, string.Empty));
  64. }
  65. }
  66. /// <summary>
  67. /// Exctracts the information from the Exception object.
  68. /// </summary>
  69. /// <param name="exception"></param>
  70. /// <param name="indent"></param>
  71. /// <returns></returns>
  72. private static string GetExceptionInfo(Exception exception, string indent)
  73. {
  74. var text = new StringBuilder();
  75. if (exception != null)
  76. {
  77. text.Append(string.Concat("Exception of type ", exception.GetType().FullName, "\r\n"));
  78. text.Append(string.Concat("Message: \"", exception.Message, "\"\r\n"));
  79. var se = exception as System.Net.Sockets.SocketException;
  80. if (se != null)
  81. {
  82. text.Append(string.Concat("Socket ErrorCode: ", se.ErrorCode, "\r\n"));
  83. text.Append(string.Concat("Socket NativeErrorCode: ", se.NativeErrorCode, "\r\n"));
  84. }
  85. text.Append(string.Concat("StackTrace:\r\n", exception.StackTrace, "\r\n"));
  86. if (exception.InnerException != null)
  87. {
  88. text.Append("Inner Exception:\r\n");
  89. text.Append(GetExceptionInfo(exception.InnerException, " "));
  90. }
  91. }
  92. else
  93. text.Append("Exception object is null");
  94. var indentedText = new StringBuilder();
  95. foreach (var line in Regex.Split(text.ToString(), "\r\n"))
  96. indentedText.Append(string.Concat(indent, line, "\r\n"));
  97. return indentedText.ToString();
  98. }
  99. }
  100. }