ExceptionHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Common.Utility
  6. {
  7. public static class ExceptionHelper
  8. {
  9. public static string GetFullExceptionMessage(this Exception ex)
  10. {
  11. string message = ex.Message + "|" + ex.StackTrace;
  12. if (ex.InnerException != null)
  13. {
  14. message += "\n\t" + GetFullExceptionMessage(ex.InnerException);
  15. }
  16. return message;
  17. }
  18. public static IEnumerable<T> FindException<T>(this Exception ex) where T : Exception
  19. {
  20. var result = new List<T>();
  21. if (ex is AggregateException)
  22. {
  23. result.AddRange(((AggregateException)ex).InnerExceptions.SelectMany(x => x.FindException<T>()));
  24. }
  25. else
  26. {
  27. if (ex is T)
  28. {
  29. result.Add((T)ex);
  30. }
  31. if (ex.InnerException != null)
  32. {
  33. result.AddRange(ex.InnerException.FindException<T>());
  34. }
  35. }
  36. return result;
  37. }
  38. }
  39. }