我知道this question并且我同意答案,但是我可以使用log4net执行以下操作吗?
而不是:
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
我可以:
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
2013-04-09 12:54:47.093 INFO Main: Line 1 Line 1 Line 1
Line 2 Line 2 Line 2
Line 3 Line 3 Line 3
是否已经支持或我是否需要编写自定义appender或自定义布局?
答案 0 :(得分:12)
我不想回答我自己的问题,但由于我自己已经提出了答案,我想与你分享。
我扩展了log4net。该解决方案继承自PatternLayout,因此所有PatternLayout功能都可用。此外,还提供了新的%缩进模式。要获得上述示例中的日志记录,只需使用:
<conversionPattern value="%date - %indentation%message%newline%exception"/>
格式化异常时log4net代码很古怪(或者我不明白)。所以在这种情况下你应该总是在模式中加上%exception,因为我硬编码了“IgnoresException = false”。在IgnoresException = true的情况下,log4net完全忽略任何格式,并且您会松开缩进。
使用以下代码扩展log4net:
/// <summary>
/// Converts %indentation to string
/// </summary>
public class IndentationPatternConverter : PatternConverter
{
protected override void Convert(TextWriter writer, object state)
{
// do nothing - %indentation is used for indentation, so nothing should be written
}
}
public class IndentationPatternLayout : PatternLayout
{
private PatternConverter m_head;
public override void Format(TextWriter writer, LoggingEvent loggingEvent)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
if (loggingEvent == null)
{
throw new ArgumentNullException("loggingEvent");
}
PatternConverter c = m_head;
IndentationWriter indentationWriter = new IndentationWriter(writer);
// loop through the chain of pattern converters
while (c != null)
{
if (c is IndentationPatternConverter)
{
indentationWriter.SetIndentation();
}
c.Format(indentationWriter, loggingEvent);
c = c.Next;
}
indentationWriter.Finish();
}
override public void ActivateOptions()
{
PatternParser patternParser = CreatePatternParser(ConversionPattern);
ConverterInfo converterInfo = new ConverterInfo()
{
Name = "indentation",
Type = typeof(IndentationPatternConverter)
};
patternParser.PatternConverters.Add("indentation", converterInfo);
m_head = patternParser.Parse();
PatternConverter curConverter = m_head;
this.IgnoresException = false;
}
}
public class IndentationWriter : TextWriter
{
TextWriter writer;
int indentation = 0;
List<string> lines = new List<string>();
public IndentationWriter(TextWriter writer)
{
this.writer = writer;
}
public override Encoding Encoding
{
get { return writer.Encoding; }
}
public override void Write(string value)
{
string[] values = value.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
for (int i = 0; i < values.Length; i++)
{
if (i > 0) values[i] = Environment.NewLine + values[i];
}
lines.AddRange(values);
}
public void Finish()
{
for (int i = 0; i < lines.Count; i++)
{
string line = lines[i];
if (i < lines.Count - 1) line = lines[i].Replace(Environment.NewLine, Environment.NewLine + new string(' ', indentation));
writer.Write(line);
}
lines.Clear();
}
public override void WriteLine(string value)
{
this.Write(value + Environment.NewLine);
}
public void SetIndentation()
{
foreach (string line in lines)
{
indentation += line.Length;
}
}
}