c# - 获得 Trace.indent 和 trace.unindent 等价物的 log4net 模式是什么

标签 c# vb.net c#-4.0 log4j log4net

我需要像 native 一样的缩进和非缩进处理 trace class .有什么想法可以用 log4net 文件和控制台 appender 完成吗?谢谢

最佳答案

我建议将 log4net 控制台附加程序包装在一个类中,并在其中添加缩进支持。我们用 StringBuilder 做一些类似的事情。我们创建了一个具有增加和减少缩进级别方法的 FormattedStringBuilder 类

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);
        }
    }
}

关于c# - 获得 Trace.indent 和 trace.unindent 等价物的 log4net 模式是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7248764/

相关文章:

.net - 在 WinForm 应用程序中对 DataGridView 进行排序时选定的行

c# - 基于条件的linq语法调用方法

C# 长子串函数

C# 同义枚举处理

regex - 使用 Regex 和 VB.NET 将字符串拆分为多个空格

c# - C# 中的动态和泛型

c# - 在文本文件中搜索直到特定字符串

c# - 使用 UTF-8 在页面标题 ASP.NET 中编码西类牙文特殊字符

c# - 如何编写一个vb.net代码来编译C/C++程序?

vb.net - 允许最终用户移动控件