ExceptionExtensions.cs 796 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Grpc.Extension.Common
  5. {
  6. public static class ExceptionExtensions
  7. {
  8. /// <summary>
  9. /// 返回一个FlatException
  10. /// </summary>
  11. /// <param name="ex"></param>
  12. /// <returns></returns>
  13. public static string GetFlatException(this Exception ex)
  14. {
  15. var exception = "";
  16. if (ex is AggregateException aex)
  17. {
  18. foreach (var e in aex.Flatten().InnerExceptions)
  19. {
  20. exception += e?.ToString() + Environment.NewLine;
  21. }
  22. }
  23. else
  24. {
  25. exception = ex.ToString();
  26. }
  27. return exception;
  28. }
  29. }
  30. }