ExceptionExc.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System;
  2. namespace Quadarax.Foundation.Core.Exceptions
  3. {
  4. public static class ExceptionExc
  5. {
  6. public static int GetCode(this Exception e)
  7. {
  8. if (!e.Data.Contains("Code"))
  9. return -1;
  10. return int.Parse(e.Data["Code"]?.ToString() ?? "-1");
  11. }
  12. public static void SetCode(this Exception e, int code)
  13. {
  14. e.Data.Add("Code", code);
  15. }
  16. public static string Dump(this Exception e)
  17. {
  18. var level = 0;
  19. var sb = new System.Text.StringBuilder();
  20. sb.Append("[#").Append(level).Append("]:").AppendLine(e.Message);
  21. var inner = e.InnerException;
  22. while (inner != null)
  23. {
  24. sb.Append("[#").Append(level).Append("]:").AppendLine(inner.Message);
  25. inner = inner.InnerException;
  26. }
  27. if (e.StackTrace != null)
  28. {
  29. sb.AppendLine("[#0] Stack list:");
  30. sb.AppendLine(e.StackTrace);
  31. }
  32. return sb.ToString();
  33. }
  34. }
  35. }