How do I use a date pattern in a header/footer?

User picture User · Nov 10, 2009 · Viewed 18k times · Source

Here's my appender configuration from my app.config. This just prints out the literal string instead of translating it to the date (i.e., it literally prints "[START: %date{MM/dd/yy HH:mm} ]").

<appender name="RollingLogFileAppender"
          type="log4net.Appender.RollingFileAppender">
  <file value="C:\somelog" />
  <appendToFile value="true" />
  <rollingStyle value="Date" />
  <datePattern value="-yyyy-MM-dd'.txt'" />
  <layout type="log4net.Layout.PatternLayout">
    <header value="[START:  %date{MM/dd/yy HH:mm} ]&#13;&#10;" />
    <conversionPattern value="%date{yyyy-MM-dd HH:mm:ss} - %message" />
    <footer value="[END]&#13;&#10;&#13;&#10;" />
  </layout>
</appender>

How can I get this to print the date/time in the header?

Answer

Puterdo Borato picture Puterdo Borato · Oct 15, 2012

Answer from here.

<header value="[BEGIN LOGGING AT %date]%newline" type="log4net.Util.PatternString" />
<footer value="[END LOGGING AT %date]%newline" type="log4net.Util.PatternString" />

Works for me like a charm. No need to write a piece of code.

Also in projects we usually use:

<header type="log4net.Util.PatternString" value="Our Application Name version %property{Assembly.Version}, .NET version %property{Runtime.Version}, %date ***%newline"/>

Take a look at PatternString doc also.

Also I've noticed that log file won't appear until you write first log statement.