tabs ↹ over ␣ ␣ ␣ spaces

by Jiří {x2} Činčura

Easy indentation control for Debug/Trace/…

16 May 2011 1 mins .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
}

Profile Picture Jiří Činčura is .NET, C# and Firebird expert. He focuses on data and business layers, language constructs, parallelism, databases and performance. For almost two decades he contributes to open-source, i.e. FirebirdClient. He works as a senior software engineer for Microsoft. Frequent speaker and blogger at www.tabsoverspaces.com.