Easy indentation control for Debug/Trace/…
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
}