开发者

How to add a blank line in the log file using log4net?

I used RollingFileAppender. And I want add a blank line to the log when my program launches. How to do that? Thanks.

Edit: OK, thanks for you all. Sorry for the confused question I asked. Let me make some explanation. I config the log4net as follows:

<log4net>
  <appender name="MyFileAppender" type="log4net.Appender.RollingFileAppender">
    <param name="File" value="ClientLog.log" />
    <param name="AppendToFile" value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%date{yyyy/MM/dd HH:mm:ss},%5p,%m开发者_开发问答%n" />
    </layout>
  </appender>

  <logger name="GlobalUse" >
    <level value="Info"/>
    <appender-ref ref="MyFileAppender" />
  </logger>

</log4net>

and the log will be:

2010/03/27 13:55:27, INFO, Program start.
2010/03/27 13:55:29, INFO, Program end.
2010/03/27 13:56:30, INFO, Program start.
2010/03/27 13:56:32, INFO, Program end.

What i hope is make the log looks like this:

2010/03/27 13:55:27, INFO, Program start.
2010/03/27 13:55:29, INFO, Program end.

2010/03/27 13:56:30, INFO, Program start.
2010/03/27 13:56:32, INFO, Program end.

2010/03/27 13:57:30, INFO, Program start.
...

Any idea? Thanks.


The selected answer is MUCH more difficult than it needs to be. I'm not sure if it didn't exist when the question was originally asked, but the correct way to do this is with a <header> or <footer> in the appender's layout. Something like this:

<layout type="log4net.Layout.PatternLayout">
  <footer value="&#13;&#10;" />
  <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>

That will insert a blank line as a footer to each logging run. More detail is here: http://logging.apache.org/log4net/release/faq.html#layout-header-xml-newlines


Log.Debug(Environment.Newline);


Change

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%message%newline" />
</layout>

with

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%message%newline%newline" />
</layout>

This will add two newlines at the end of file. First one will be there as a new Line , second one acts as a starting point of the next log.


You would need a special appender. Here you have a configuration example


<log4net>
  ...
  <appender name="MyRollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="C:\temp\mylog.log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyyMMdd" />
    <staticLogFileName value="true" />
    <maximumFileSize value="2MB" />
    <maxSizeRollBackups value="20" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message%newline" />
    </layout>
  </appender>

  <logger name="MyLogger">
    <level value="Info" />
    <appender-ref ref="MyRollingLogFileAppender" />
  </logger>  
  ...
</log4net>

This configuration allows the insert of a WHITE LINE, COMPLETELY WHITE (<conversionPattern value="%message%newline" />)

And the code to log would be

LogManager.GetLogger("MyLogger").Info("");

A last comment: this allows you to do what I think you want but I wouldn't do it in my own development :-) if I misunderstood your question please let me know

EDIT1 This appedender / logger is ADDED to your existing configuration. It would be ONLY used to generate the white line you need. For the rest of the logging you would use your previously existing logger / appender,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜