How to indent a Dump() result?
Options
If I want to output a log using Dump()
in the hierarchical structure. I want to indent some of the Dump results. Is that possible?
Comments
-
Would something like this work?
void Main() { var f = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat; f.FirstDayOfWeek.Dump(""); f.NativeCalendarName.DumpIndented(1); f.FullDateTimePattern.DumpTellIndented(1); f.LongDatePattern.DumpTellIndented(2); f.Calendar.DumpIndented(3, "Calendar", collapseTo:1); f.AbbreviatedDayNames.DumpTellIndented(2,collapseTo: 0); } public static class Indent { public static int SizeOfIndent = 10; public static T DumpIndented<T>(this T obj, int indentLevel = 0, string? description = null, int? collapseTo = null) { string indent = " ".PadLeft(indentLevel * SizeOfIndent); if (description != null) Util.HorizontalRun(true, indent, Util.WithHeading(obj, description)).Dump(collapseTo: collapseTo); else Util.HorizontalRun(true, indent, obj).Dump(collapseTo: collapseTo ); return obj; } public static T DumpTellIndented<T>(this T obj, int indentLevel = 0, [System.Runtime.CompilerServices.CallerArgumentExpression("obj")] string? autoGeneratedDescription = null, int? collapseTo = null) { string indent = " ".PadLeft(indentLevel * SizeOfIndent); Util.HorizontalRun(true, indent, Util.WithHeading(obj, autoGeneratedDescription)).Dump(collapseTo: collapseTo); return obj; } }
Results look like
Of course there are a whole load of more optional parameters available with dump and I have just using two, but it is probably easy to add most of the rest of them.
-
Yes, something like that. I hope that feature can be built-in to LINQPad.
Usage example:
a.Dump(indent: 1); if (a == "") { b.Dump(indent: 2); }
Or like this:
using (var scope1 = new Util.DumpScope()) { a.Dump(); // auto-indented 1 level using (var scope2 = new Util.DumpScope()) { b.Dump(); // auto-indented 2 level } }