public static string Format( this Exception exception, ErrorFormattingOption option = Default, char replaceWith = '-') { return(FormatLazy(exception, option, replaceWith)); }
private static readonly Regex CHAR_OR_NUMERIC = new Regex(@"[\w|\d]"); // char or digit #region Format / FormatLazy /// <summary> /// Simplify the exception formatting. /// Make async exception read-able. /// Will do the formating on ToSting timing. /// This can reduce compute and memory pressure when the exception send to /// semantical logging where it might be filter out and won't be present. /// </summary> /// <param name="exception">The exception.</param> /// <param name="option">Formatting option.</param> /// <param name="replaceWith">The replacement char.</param> /// <returns>Formatted exception details</returns> public static LazyFormatException FormatLazy( this Exception exception, ErrorFormattingOption option = Default, char replaceWith = '-') { return(new LazyFormatException(exception, option, replaceWith)); }
/// <summary> /// Initializes a new instance of the <see cref="LazyFormatException" /> class. /// </summary> /// <param name="innerException">The inner exception.</param> /// <param name="option">Formatting option.</param> /// <param name="replaceWith">The replacement char.</param> public LazyFormatException( Exception innerException, ErrorFormattingOption option = Default, char replaceWith = '-') : base(string.Empty, innerException) { _option = option; _replaceWith = replaceWith; }
private static async Task FormatAsync(int j, ErrorFormattingOption options = ErrorFormattingOption.Default) { try { //await Task.Run(() => throw new ArgumentException("Other Error")); await Step1Async(j); } catch (Exception ex) { Console.WriteLine(ex.Format(options)); //Console.WriteLine(ex.FormatWithLineNumber()); } }
/// <summary> /// Simplify the exception. /// </summary> /// <param name="exception">The exception.</param> /// <param name="option">Formatting option.</param> /// <param name="includeFullUnformatedDetails">if set to <c>true</c> [include exception.ToString()].</param> /// <param name="replaceWith">The replacement char.</param> /// <returns></returns> public static string Format( this Exception exception, ErrorFormattingOption option, bool includeFullUnformatedDetails = false, char replaceWith = '-', bool withLineNumber = false) { if (exception == null) { return(string.Empty); } try { var builder = new StringBuilder(); builder.AppendLine("Root cause:"); var aggregate = exception as AggregateException; if (aggregate == null) { var root = exception?.GetBaseException(); var rootMessage = root?.Message; builder.AppendLine($"\t{rootMessage}\r\n"); } else { foreach (var ex in aggregate.Flatten().InnerExceptions) { var root = ex?.GetBaseException(); var rootMessage = root?.Message; builder.AppendLine($"\t{rootMessage}\r\n"); } } builder.AppendLine("Formatted Stacks"); List <string> keep = FormaStack(exception, withLineNumber); string prev = null; int lastCount = 0; for (int i = 0; i < keep.Count; i++) { // TODO: try to capture the parameters string candidate = keep[i]; string origin = candidate; if (option == ErrorFormattingOption.FormatDuplication) { if (origin.StartsWith(THROW_PREFIX)) { prev = null; } else { int count = 0; if (prev != null) { (candidate, count) = HideDuplicatePaths(prev, candidate, replaceWith); } prev = origin; if (lastCount > count) { candidate = origin; // when similarity decreased, it write the full information lastCount = 0; } else { lastCount = count; } } } builder.Append(candidate); } if (includeFullUnformatedDetails) { builder.AppendLine("====================== FULL INFORMATION ============================"); builder.AppendLine(exception.ToString()); builder.AppendLine("===================================================================="); } return(builder.ToString()); } catch { return(exception.ToString()); } }