CatchUnhandledException.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. static NLog.Logger logger = NLog.LogManager.LoadConfiguration("nlog.config").GetLogger("Application");
  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. logger.Error(GetExceptionInfo(oException as Exception, string.Empty));
  60. //FileSupport.EnsureDirectoryExists(@"\Flash\Logs\");
  61. //using (var sw = new StreamWriter(string.Format(@"\Flash\Logs\CrashLog_{0}.txt",
  62. // DateTime.Today.ToString("yyyyMMdd")), true))
  63. //{
  64. // sw.WriteLine("**********************************************************************");
  65. // sw.WriteLine(string.Format("* {0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
  66. // sw.Write(GetExceptionInfo(oException as Exception, string.Empty));
  67. //}
  68. }
  69. /// <summary>
  70. /// Exctracts the information from the Exception object.
  71. /// </summary>
  72. /// <param name="exception"></param>
  73. /// <param name="indent"></param>
  74. /// <returns></returns>
  75. private static string GetExceptionInfo(Exception exception, string indent)
  76. {
  77. var text = new StringBuilder();
  78. if (exception != null)
  79. {
  80. text.Append(string.Concat("Exception of type ", exception.GetType().FullName, "\r\n"));
  81. text.Append(string.Concat("Message: \"", exception.Message, "\"\r\n"));
  82. var se = exception as System.Net.Sockets.SocketException;
  83. if (se != null)
  84. {
  85. text.Append(string.Concat("Socket ErrorCode: ", se.ErrorCode, "\r\n"));
  86. text.Append(string.Concat("Socket NativeErrorCode: ", se.NativeErrorCode, "\r\n"));
  87. }
  88. text.Append(string.Concat("StackTrace:\r\n", exception.StackTrace, "\r\n"));
  89. if (exception.InnerException != null)
  90. {
  91. text.Append("Inner Exception:\r\n");
  92. text.Append(GetExceptionInfo(exception.InnerException, " "));
  93. }
  94. }
  95. else
  96. text.Append("Exception object is null");
  97. var indentedText = new StringBuilder();
  98. foreach (var line in Regex.Split(text.ToString(), "\r\n"))
  99. indentedText.Append(string.Concat(indent, line, "\r\n"));
  100. return indentedText.ToString();
  101. }
  102. }
  103. }