Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the log4net pattern to get the equivalent of Trace.indent and trace.unindent

I need indent and unindent handling like the native trace class. Any ideas how this can done with log4net file and console appender ? thank you

like image 372
Gullu Avatar asked Aug 30 '11 19:08

Gullu


1 Answers

I would recommend wraping the log4net console appender in a class and adding the indent support there. We do something similiar with StringBuilder. We created a FormattedStringBuilder class that has Increase and Decrease Indent level methods

private const string Indent = "\t";
private readonly int IndentLength = Indent.Length;

public void IncreaseIndent()
{
    // Increase indent
    indentLevel++;
    indentBuffer.Append(Indent);

    // If new line already started, insert another indent at the beginning
    if (!useIndent)
    {
        contentBuffer.Insert(0, Indent);
    }
}

public void DecreaseIndent() 
{
    // Only decrease the indent to zero.
    if (indentLevel > 0) 
    {
        indentLevel--;

        // Remove an indent from the string, if applicable
        if (indentBuffer.Length != 0) 
        {
            indentBuffer.Remove(indentBuffer.Length - IndentLength, IndentLength);
        }
    }
}
like image 77
Brad Patton Avatar answered Oct 17 '22 20:10

Brad Patton