<PackageReference Include="NUnit" Version="3.8.0" />

ExceptionHelper

public class ExceptionHelper
ExceptionHelper provides static methods for working with exceptions
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Runtime.ExceptionServices; using System.Text; namespace NUnit.Framework.Internal { public class ExceptionHelper { public static void Rethrow(Exception exception) { ExceptionDispatchInfo.Capture(exception).Throw(); } public static string BuildMessage(Exception exception, bool excludeExceptionNames = false) { StringBuilder stringBuilder = new StringBuilder(); if (!excludeExceptionNames) stringBuilder.AppendFormat("{0} : ", exception.GetType()); stringBuilder.Append(GetExceptionMessage(exception)); foreach (Exception item in FlattenExceptionHierarchy(exception)) { stringBuilder.Append(Environment.NewLine); stringBuilder.Append(" ----> "); if (!excludeExceptionNames) stringBuilder.AppendFormat("{0} : ", item.GetType()); stringBuilder.Append(GetExceptionMessage(item)); } return stringBuilder.ToString(); } public static string BuildStackTrace(Exception exception) { StringBuilder stringBuilder = new StringBuilder(GetSafeStackTrace(exception)); foreach (Exception item in FlattenExceptionHierarchy(exception)) { stringBuilder.Append(Environment.NewLine); stringBuilder.Append("--"); stringBuilder.Append(item.GetType().Name); stringBuilder.Append(Environment.NewLine); stringBuilder.Append(GetSafeStackTrace(item)); } return stringBuilder.ToString(); } private static string GetSafeStackTrace(Exception exception) { try { return exception.StackTrace; } catch (Exception) { return "No stack trace available"; } } private static string GetExceptionMessage(Exception ex) { if (string.IsNullOrEmpty(ex.Message)) { FileNotFoundException ex2 = ex as FileNotFoundException; if (ex2 == null) return "No message provided"; return "Could not load assembly. File not found: " + ex2.FileName; } return ex.Message; } private static List<Exception> FlattenExceptionHierarchy(Exception exception) { List<Exception> list = new List<Exception>(); if (exception is ReflectionTypeLoadException) { ReflectionTypeLoadException ex = exception as ReflectionTypeLoadException; list.AddRange(ex.LoaderExceptions); Exception[] loaderExceptions = ex.LoaderExceptions; foreach (Exception exception2 in loaderExceptions) { list.AddRange(FlattenExceptionHierarchy(exception2)); } } if (exception is AggregateException) { AggregateException ex2 = exception as AggregateException; list.AddRange(ex2.InnerExceptions); { foreach (Exception innerException in ex2.InnerExceptions) { list.AddRange(FlattenExceptionHierarchy(innerException)); } return list; } } if (exception.InnerException != null) { list.Add(exception.InnerException); list.AddRange(FlattenExceptionHierarchy(exception.InnerException)); } return list; } } }