在log4net中以非日志行格式 [英] Formatting in non-log lines in log4net
问题描述
我想在FileAppender记录器中对文件名和日志文件的页眉/页脚部分进行一些调整.
鉴于以下配置部分:
I would like to make some adjustments to the the name of the file and the header/footer sections of the log file in my FileAppender logger.
Given the following config section:
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value=".\ImportLog.log"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="== Begin Processing ===================="/>
<param name="Footer" value="== Completed Processing ================"/>
<conversionPattern value="%date{HH:mm:ss},%message,%-5level,%logger%newline"/>
</layout>
</appender>
我尝试这样做:
I tried to do this:
<file value=".\%date{HHmmss}ImportLog.log"/>
...但是我的文件名实际上是%date {HHmmss} ImportLog.log".
我希望我的页眉和页脚在它们的上方和下方都有一个空格,因此我尝试了以下操作:
...but my filename was literally "%date{HHmmss}ImportLog.log".
I want my header and footer to have a line space above and below them, so I tried this:
<param name="Header" value="== Begin Processing ====================%newline"/>
<param name="Footer" value="== Completed Processing ================%newline"/>
...但是我只是在这些行之后得到了文本%newline".在这种情况下,\ r \ n也显示为文字.
有没有办法对这些非日志行进行模板化(或在运行时修改)?
...but I just get the text "%newline" after those lines. In this instance, \r\n also appear as literals.
Is there a way to template or (modify at runtime) these non-log lines?
推荐答案
找到了 ^ ]:从PatternLayout
派生.
我最终这样做了:
Found a solution[^]: derive fromPatternLayout
.
I ended up doing this:
public class TimestampedPatternLayout : PatternLayout
{
public override string Header
{
get
{
string result = "== Processing Started ({0}) ==============\r\n";
result = string.Format(result, DateTime.Now.ToString("HH:mm:ss"));
return result;
}
set { }
}
public override string Footer
{
get
{
string result = "== Processing Ended ({0}) ================\r\n";
result = string.Format(result, DateTime.Now.ToString("HH:mm:ss"));
return result;
}
set { }
}
}
这也简化了我的配置:
Which also simplified my config:
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value=".\ImportLog.log"/>
<appendToFile value="true"/>
<layout type="Engage.ImportLegacy.TimestampedPatternLayout">
<conversionPattern value="%date{HH:mm:ss},%message,%-5level,%logger%newline"/>
</layout>
</appender>
干杯.
Cheers.
这篇关于在log4net中以非日志行格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!