tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Easy indentation control for Debug/Trace/...

16 May 2011 .NET, C#

I was setting up simple command logging, but keeping in my mind where I increased the indentation to later decrease it back was causing me headache. Also formatting the string I’d like to put out wasn’t smooth.

So I created simple wrapper class implementing IDisposable, where the disposing will actually decrease the indentation automatically. Only thing you (I) have to use is simple using block. The precooked WriteLine helper method is there just to save me some typing.

class IndentHolder : IDisposable
{
	public void Dispose()
	{
		Trace.Unindent();
	}
}
public static void WriteLine(string format, string category, params object[] args)
{
#if (TRACE)
	Trace.WriteLine(string.Format(format, args), category);
#endif
}
public static IDisposable Indent()
{
#if (TRACE)
	Trace.Indent();
	return new IndentHolder();
#endif
}