Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 620
Jollian Avatar asked Mar 27 '10 01:03

Jollian


Video Answer


1 Answers

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

like image 117
Nick Avatar answered Oct 01 '22 05:10

Nick