Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 804
User Avatar asked Nov 10 '09 22:11

User


People also ask

How do I change the date format in a header in Word?

In the toolbar, click the Header icon, and choose your header type. Click the Insert tab. Click Date and Time, choose your Date and Time style, and then click OK.

How do I change the date format in a header?

Open the Format Cells Dialogue Box by right clicking in the selection and choosing Format Cells. Select the Number tab, and Custom. In the Type: line, enter dd-mmm-yy as below and click OK. Was this reply helpful?


2 Answers

Building on @Wizou's comment. See the DynamicPatternLayout Class documentation.

I'm actually using this difference in behaviour to have a start and end time

One important difference between PatternLayout and DynamicPatternLayout is the treatment of the Header and Footer parameters in the configuration. The Header and Footer parameters for DynamicPatternLayout must be syntactically in the form of a PatternString, but should not be marked as type log4net.Util.PatternString. Doing so causes the pattern to be statically converted at configuration time and causes DynamicPatternLayout to perform the same as PatternLayout.

Setting the type on Header to PatternString but leaving Footer as dynamic

<layout type="log4net.Layout.DynamicPatternLayout"> <param name="Header" value="%newline**** Trace Opened Local: %date{yyyy-MM-dd HH:mm:ss.fff} UTC: %utcdate{yyyy-MM-dd HH:mm:ss.fff} ****%newline" type="log4net.Util.PatternString" /> <param name="Footer" value="**** Trace Closed %date{yyyy-MM-dd HH:mm:ss.fff} ****%newline" /> </layout>

like image 166
k3yz101 Avatar answered Nov 03 '22 08:11

k3yz101


Answer from here.

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

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.

like image 41
4 revs, 3 users 78% Avatar answered Nov 03 '22 10:11

4 revs, 3 users 78%