Exemple #1
0
        /// <summary>
        /// Make sure that relevant details are extract from exceptions!
        /// </summary>
        /// <param name="e"></param>
        /// <param name="sb"></param>
        protected void DumpException(Exception e, StringBuilder sb, int depth = 0)
        {
            if (sb == null)
            {
                sb = new StringBuilder();
            }

            string prefix = string.Concat(Enumerable.Repeat("  ", depth));

            sb.AppendLine(prefix + $"{e.GetType().FullName} [{(uint)e.HResult}]");
            sb.AppendLine(prefix + (string.IsNullOrWhiteSpace(e.Message) ? "No message" : e.Message));
            string cleanTrace = ErrorUtils.CleanStackTrace(e.StackTrace);

            sb.AppendLine(prefix + (string.IsNullOrWhiteSpace(cleanTrace) ? "No stack trace" : cleanTrace));

            if (e is ReflectionTypeLoadException reflectionTypeLoadException)
            {
                foreach (var loaderException in reflectionTypeLoadException.LoaderExceptions)
                {
                    sb.AppendLine(prefix + "Loader exception:" + loaderException.Message);

                    if (loaderException.InnerException != null)
                    {
                        this.DumpException(loaderException.InnerException, sb, depth + 1);
                    }
                }
            }

            if (e is BadImageFormatException badImageFormatException)
            {
                sb.AppendLine(prefix + "FileName: " + badImageFormatException.FileName);
                sb.AppendLine(prefix + "FusionLog: " + badImageFormatException.FusionLog);
                sb.AppendLine(prefix + "Environment.Is64BitProcess: " + (Environment.Is64BitProcess ? "yes" : "no"));
            }

            if (e is FileNotFoundException fileNotFoundException)
            {
                sb.AppendLine(prefix + "FusionLog: " + fileNotFoundException.FusionLog);
                sb.AppendLine(prefix + "FileName: " + fileNotFoundException.FileName);
            }

            if (e is AggregateException aggregateException)
            {
                foreach (var innerAggregate in aggregateException.InnerExceptions)
                {
                    sb.AppendLine(prefix + "Aggregate Exception");
                    this.DumpException(innerAggregate, sb, depth + 1);
                }
            }

            if (e.InnerException != null)
            {
                sb.AppendLine(prefix + "Inner Exception");
                this.DumpException(e.InnerException, sb, depth + 1);
            }
        }