ExceptionHelper.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. #pragma warning disable CS8604 // Possible null reference argument.
  7. namespace EasyDevCore.Common
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public static class ExceptionHelper
  13. {
  14. /// <summary>
  15. /// Gets the stack trace of exception.
  16. /// </summary>
  17. /// <param name="ex">The ex.</param>
  18. /// <param name="msg">The MSG.</param>
  19. private static void GetExceptionStackTrace(Exception ex, ref StringBuilder msg)
  20. {
  21. msg.Append(ex.StackTrace);
  22. if (ex.InnerException != null)
  23. {
  24. msg.AppendLine(ex.InnerException.Message);
  25. GetExceptionStackTrace(ex.InnerException, ref msg);
  26. }
  27. }
  28. /// <summary>
  29. /// Gets the stack trace of exception.
  30. /// </summary>
  31. /// <param name="ex">The ex.</param>
  32. /// <returns></returns>
  33. private static string GetExceptionStackTrace(Exception ex)
  34. {
  35. StringBuilder msg = new StringBuilder();
  36. GetExceptionStackTrace(ex, ref msg);
  37. return msg.ToString();
  38. }
  39. /// <summary>
  40. /// Gets the exception info.
  41. /// </summary>
  42. /// <param name="exception">The exception.</param>
  43. /// <returns></returns>
  44. public static string GetMessagesDetail(this Exception exception)
  45. {
  46. if (exception != null)
  47. {
  48. StringBuilder buidler = new StringBuilder();
  49. buidler.AppendLine(string.Format("---- Begin of Exception -----", exception.Message));
  50. buidler.AppendLine(string.Format("+ Message : {0}", exception.Message));
  51. buidler.AppendLine(string.Format("+ Source : {0}", exception.Source));
  52. buidler.AppendLine(string.Format("+ Stack : {0}", exception.StackTrace));
  53. buidler.AppendLine(string.Format("+ Target : {0}", (exception.TargetSite == null ? "?" : exception.TargetSite.ToString())));
  54. buidler.AppendLine(string.Format("+ Helplink : {0}", exception.HelpLink));
  55. buidler.AppendLine("+ Stack : ");
  56. buidler.AppendLine("----- Begin of exception stack -----");
  57. buidler.AppendLine(GetExceptionStackTrace(exception));
  58. buidler.AppendLine("----- End of exception stack -----");
  59. if (exception.Data != null && exception.Data.Keys.Count > 0)
  60. {
  61. buidler.AppendLine("----- Begin of exception data info -----");
  62. foreach (var key in exception.Data.Keys)
  63. {
  64. buidler.AppendLine(string.Format(">>>>>>>>>> [{0}] <<<<<<<<<<", key));
  65. buidler.AppendLine(string.Format("{0}", exception.Data[key]));
  66. buidler.AppendLine(string.Format("^^^^^^^^^^ [{0}] ^^^^^^^^^^", key));
  67. buidler.AppendLine(string.Format("---- End of Exception -----", exception.Message));
  68. }
  69. buidler.AppendLine("----- End of exception data info -----");
  70. }
  71. buidler.AppendLine(string.Format("---- End of Exception -----", exception.Message));
  72. return buidler.ToString();
  73. }
  74. return string.Empty;
  75. }
  76. }
  77. }