CatchUnhandledException.cs 3.9 KB

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