You need to be using log4net 1.2.9 for the PatternString stuff to be
supported, could you check that you are using this version.
Also the Footer is not going to give you the time that the file is
closed, but exactly the same time that is written to the header! This is
because the PatternStrings are evaluated at configuration time, i.e.
%date will expand out to the time the config was loaded (or reloaded!).
This is either a future or a flaw depending on your point of view!
If you need the header and footer to always include the current time
then you need to subclass the PatternLayout as follows:
using System;
using log4net.Layout;
namespace TestConsoleApp
{
public class MyPatternLayout : PatternLayout
{
public override string Header
{
get
{
return "[BEGIN LOGGING AT "+ DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss,fff") +"]";
}
set { }
}
public override string Footer
{
get
{
return "[END LOGGING AT "+ DateTime.Now.ToString("yyyy-MM-dd
HH:mm:ss,fff") +"]";
}
set { }
}
}
}
Add this to your assembly, then in the config file change the type of
the <layout> to point to your new derived type.
Cheers,
Nicko
> -----Original Message-----
> From: Russell Haley [mailto:[EMAIL PROTECTED]
> Sent: 16 June 2005 17:28
> To: Log4NET User
> Subject: RE: Date Time in Header Footer
>
> k, this is embarassing now... I've tried every combination I
> could think of and I'm still getting the same result. Neither
> the datetime output nor the %newline are working for me. What
> the hell am I doing wrong??? I feel like such a poser!
> ("hey, maybe I'll just get the log4net guys to write the
> WHOLE application for me!") Here is my config file and below
> that is the output I get:
>
> <log4net>
> <appender name="SystemEventInfoLog"
> type="log4net.Appender.RollingFileAppender">
> <param name="File" value="../logging/" />
> <param name="AppendToFile" value="true" />
> <param name="RollingStyle" value="Date" />
> <param name="MaxSizeRollBackups" value="10" />
> <param name="DatePattern"
> value="yyyy/MMMMMMMMM/yyyy-MM-dd.\\I\\N\\F\\O"/>
> <param name="StaticLogFileName" value="False" />
> <filter type="log4net.Filter.LevelMatchFilter">
> <LevelToMatch value="INFO" />
> </filter>
> <filter type="log4net.Filter.DenyAllFilter" />
> <layout type="log4net.Layout.PatternLayout">
> <Header value="[BEGIN LOGGING
> AT \date ] %n "
> type="log4net.Util.PatternString" />
> <Footer value="[END LOGGING AT \d ]\r\n"
> type="log4net.Util.PatternString" />
> <param name="ConversionPattern"
> value="%d - %m%n" />
> </layout>
> </appender>
> <appender name="SystemLog"
> type="log4net.Appender.RollingFileAppender">
> <param name="File" value="../logging/" />
> <param name="Threshold" value="ALL" />
> <param name="AppendToFile" value="true" />
> <param name="RollingStyle" value="Date" />
> <param name="MaxSizeRollBackups" value="10" />
> <param name="DatePattern"
> value="yyyy/MMMMMMMMM/yyyy-MM-dd.\\S\\Y\\S\\T\\E\\M"/>
> <param name="StaticLogFileName" value="False" />
> <layout type="log4net.Layout.PatternLayout">
> <header value="[BEGIN LOGGING
> AT %date ]%n"
> type="log4net.Util.PatternString" />
> <footer value="[END LOGGING AT
> %d ]%newline"
> type="log4net.Util.PatternString" />
> <param name="ConversionPattern"
> value="%d [%t] %-5p %c [%x] - %m%n" />
> </layout>
> </appender>
>
> <!-- Set root logger level to DEBUG and its
> only appender to A1 -->
> <root>
> <appender-ref ref="SystemLog" />
> <appender-ref ref="SystemEventInfoLog"/>
> </root>
> </log4net>
>
>
>
> Output from the SystemEventInfoLog appender:
>
> [BEGIN LOGGING AT %date ] %n [END LOGGING AT %d ] [BEGIN
> LOGGING AT %date ] %n [END LOGGING AT %d ] [BEGIN LOGGING AT
> %date ] %n [END LOGGING AT %d ]
>
> /*******************************************************
>
> Output from System Log appender:
>
> [BEGIN LOGGING AT %date ]%n[END LOGGING AT %d ]%newline[BEGIN
> LOGGING AT %date ]%n[END LOGGING AT %d ]%newline
>
> /*******************************************************
> I'm going to be shot if my manager finds out I spent an hour
> testing header/footer dates this morning. These dates are
> rather important, though, because they tell me when the
> application was started and when the first errors started to
> occur. This app may be left running independantly for weeks
> on end and eventually the logic will be moved into a window service.
> :}
>
> Thank you for your patience.
> Russ
>
>
> -----Original Message-----
> From: Ron Grabowski [mailto:[EMAIL PROTECTED]
> Sent: 16-Jun-05 6:12 AM
> To: Log4NET User
> Subject: Re: Date Time in Header Footer
>
>
> I found these sentances helpful:
>
> "
> The goal of this class is to Format a LoggingEvent as a
> string. The results depend on the conversion pattern.
> "
> http://tinyurl.com/e3nd3
> http://logging.apache.org/log4net/release/sdk/log4net.Layout.P
> atternLayout.h
> tml
>
> "
> Unlike the PatternLayout however the PatternString does does
> not render properties of a specific LoggingEvent but of the
> process in general.
> "
> http://tinyurl.com/dghqs
> http://logging.apache.org/log4net/release/sdk/log4net.Util.Pat
> ternString.htm
> l
>
> I used a PatternString in the header and footer nodes because
> I didn't have a LoggingEvent to pull information out of.
> PatternString and PatternLayout have some overlap with things
> like %date/%d and %newline/%n. That's where I got confused. I
> thought I could use %n and %d as shortcuts in a PatternString
> but you can't, you need to use the full %date and %newline patterns.
>
> Line 168 of log4net.Util.PatternStringConverters.DatePatternConverter
> confirms that the %date pattern of PatternString uses the current
> date/time:
>
> m_dateFormatter.FormatDate(DateTime.Now, writer);
>
> Had that been a PatternLayout, it would have used
> loggingEvent.TimeStamp for its DateTime value.
>
> Hope that helps,
> Ron
>
> --- Russell Haley <[EMAIL PROTECTED]> wrote:
>
> > Can I ask you where you found that? I was looking REALLY hard for
> > that.
> > It's upsetting when I can't find things... Now where did I put my
> > config file?
> >
> > Russ
> >
> > Ron Grabowski wrote:
> >
> > >This is what you want:
> > >
> > ><header value="[BEGIN LOGGING AT %date]%newline"
> > >type="log4net.Util.PatternString" />
> > >
> > ><footer value="[END LOGGING AT %date]%newline"
> > >type="log4net.Util.PatternString" />
> > >
> > >My example of using a custom PatternConverter is overkill.
> You don't
> > >need to do that. Same thing for my idea about having a
> %now pattern;
> > >PatternString's %date pattern already does that.
> > >
> > >I learn something new every time I read the docs :-)
> > >
> > >- Ron
> > >
> > >--- Russell Haley <[EMAIL PROTECTED]> wrote:
> > >
> > >
> > >
> > >>lol, my bad. That's probably what I did too... :-}
> > >>
> > >>Cheers
> > >>Russ
> > >>
> > >>-----Original Message-----
> > >>From: Ron Grabowski [mailto:[EMAIL PROTECTED]
> > >>Sent: 15-Jun-05 12:19 PM
> > >>To: Log4NET User
> > >>Subject: RE: Date Time in Header Footer
> > >>
> > >>
> > >>The %d pattern applies to the timestamp of the log
> message. You're
> > >>essentially wanting to print out what time log4net was configured.
> > >>Its
> > >>my understanding that the header is written before any messages
> > come
> > >>through the pipeline and the footer is written during the
> shutdown
> > >>process. I could be wrong.
> > >>
> > >>As soon as I configure log4net, I record this log message:
> > >>
> > >> log.Info("Application Started.");
> > >>
> > >>In my application's shutdown code I record this log message:
> > >>
> > >> log.Info("Application Ended.");
> > >>
> > >>So I can easily see when my application has recycled.
> > >>
> > >>- Ron
> > >>
> > >>--- Russell Haley <[EMAIL PROTECTED]> wrote:
> > >>
> > >>
> > >>
> > >>>hmmm... I could have sworn I got something like this
> working in a
> > >>>logger I wrote for a different company... The solution
> would have
> > >>>had to be really simple because I didn't know JS about L4N (not
> > >>>that my knowledge
> > of
> > >>>L4N has
> > >>>changed really...) and just used the default config info from one
> > >>>
> > >>>
> > >>of
> > >>
> > >>
> > >>>the
> > >>>examples - not including this one modification.
> > >>>
> > >>>Anyway, thanks once again Ron!
> > >>>
> > >>>Russ
> > >>>
> > >>>-----Original Message-----
> > >>>From: Ron Grabowski [mailto:[EMAIL PROTECTED]
> > >>>Sent: 15-Jun-05 11:39 AM
> > >>>To: Log4NET User
> > >>>Subject: Re: Date Time in Header Footer
> > >>>
> > >>>
> > >>>You could write your own pattern converter to expose the a
> > DateTime
> > >>>object:
> > >>>
> > >>><header type="log4net.Util.PatternString">
> > >>> <converter>
> > >>> <name value="dateTimeNow" />
> > >>> <type value="Company.Project.Logging.DateTimePatternConverter,
> > >>>Company.Project" />
> > >>> </converter>
> > >>> <conversionPattern value="[BEGIN LOGGING AT
> > >>>
> > >>>
> > >>%dateTimeNow{hh:mm}]%n"
> > >>
> > >>
> > >>>/>
> > >>></header>
> > >>>
> > >>>I agree that that's a lot of code for just wanting to print out
> > the
> > >>>current date and time :-/
> > >>>
> > >>>Perhaps a built-in pattern called %now could be added
> that accepts
> > >>>
> > >>>
> > >>a
> > >>
> > >>
> > >>>DateTime format:
> > >>>
> > >>> %now{d}
> > >>> %now{hh:mm:ss}
> > >>>
> > >>>- Ron
> > >>>
> > >>>--- Russell Haley <[EMAIL PROTECTED]> wrote:
> > >>>
> > >>>
> > >>>
> > >>>>I'm trying ot insert the date and time in the header footer
> > >>>>
> > >>>>
> > >>blocks
> > >>
> > >>
> > >>>of
> > >>>
> > >>>
> > >>>>my
> > >>>>log. I did a search on the mailing list but didn't find
> anything.
> > >>>>
> > >>>>I have tried %d, %date and \d without any success.
> > >>>>
> > >>>> <param name="Header" value="[BEGIN LOGGING AT \d]\r\n"
> /> <param
> > >>>> name="Footer" value="[END LOGGING AT \d]\r\n" />
> > >>>>
> > >>>>Suggestions?
> > >>>>
> > >>>>Thanks!
> > >>>>
> > >>>>Russ
> > >>>>
> > >>>>
> > >>>>
> > >>>>
> > >>>
> > >>>
> > >>
> > >>
> > >
> > >
> > >
> > >
> > >
> >
> >
>
>