Create a new module (I called mine DynamicPatternLayout) in the
src\layout directory. Paste this code into it.
Build and use. (As I recall that is all I did.)
(Code below has TABs in it for alignment.)
using System;
using System.Collections;
using System.IO;
using log4net.Core;
using log4net.Layout.Pattern;
using log4net.Util;
namespace log4net.Layout
{
/// <summary>
/// A flexible layout configurable with pattern string that
re-evaluates on each call.
/// </summary>
/// <remarks>
/// <para>This class is built on <see
cref="PatternLayout"></see> and provides all the
/// features and capabilites of PatternLayout. PatternLayout is
a 'static' class
/// in that its layout is done once at configuration time. This
class will recreate
/// the layout on each reference.</para>
/// <para>On important difference between PatternLayout and
DynamicPatternLayout is the
/// treatment of the Header and Foot parameters in the
configuration. The Header and Footer
/// parameters for DynamicPatternLayout must be syntactacly in
the form of a PatternString,
/// but should not be marked as type log4net.Util.PatternString.
Doing so causes the
/// pattern to be staticly converted at configuration tiame and
causes DynamicPatternLayout
/// to perform the same as PatternLayout.</para>
/// <para>Please see <see cref="PatternLayout"></see> for
complete documentation.</para>
/// </remarks>
class DynamicPatternLayout: PatternLayout
{
#region Member Variables
/// <summary>
/// The header PatternString
/// </summary>
private PatternString m_headerPatternString = new
PatternString("");
/// <summary>
/// The footer PatternString
/// </summary>
private PatternString m_footerPatternString = new
PatternString("");
#endregion
#region Constructors
/// <summary>
/// Constructs a DynamicPatternLayout using the
DefaultConversionPattern
/// </summary>
/// <remarks>
/// <para>
/// The default pattern just produces the application
supplied message.
/// </para>
/// <para>
/// Note to Inheritors: This constructor calls the
virtual method
/// <see cref="CreatePatternParser"/>. If you override
this method be
/// aware that it will be called before your is called
constructor.
/// </para>
/// <para>
/// As per the <see cref="IOptionHandler"/> contract the
<see cref="ActivateOptions"/>
/// method must be called after the properties on this
object have been
/// configured.
/// </para>
/// </remarks>
public DynamicPatternLayout()
: base()
{
}
/// <summary>
/// Constructs a DynamicPatternLayout using the supplied
conversion pattern
/// </summary>
/// <param name="pattern">the pattern to use</param>
/// <remarks>
/// <para>
/// Note to Inheritors: This constructor calls the
virtual method
/// <see cref="CreatePatternParser"/>. If you override
this method be
/// aware that it will be called before your is called
constructor.
/// </para>
/// <para>
/// When using this constructor the <see
cref="ActivateOptions"/> method
/// need not be called. This may not be the case when
using a subclass.
/// </para>
/// </remarks>
public DynamicPatternLayout (string pattern)
: base(pattern)
{
}
#endregion
#region Override implementation of LayoutSkeleton
public override string Header
{
get
{
return m_headerPatternString.Format();
}
set
{
base.Header = value;
m_headerPatternString = new
PatternString(value);
}
} /* property DynamicPatternLayout Header
*/
public override string Footer
{
get
{
return m_footerPatternString.Format();
}
set
{
base.Footer = value;
m_footerPatternString = new
PatternString(value);
}
} /* property DynamicPatternLayout Footer
*/
#endregion
} /* class DynamicPatternLayout */
} /* namespace log4net.Layout */
----------------------------------------------------------------------
Roy Chastain
-----Original Message-----
From: Wilco K [mailto:[email protected]]
Sent: Friday, October 09, 2009 04:16
To: [email protected]
Subject: RE: Header in new file
It looks there is no DynamicPatternLayout type in the log4net assembly.
Can you send me your working copy of it?
Thanks!
> Subject: RE: Header in new file
> Date: Thu, 8 Oct 2009 22:35:27 -0400
> From: [email protected]
> To: [email protected]
>
> It is possible. Example below
>
> <appender name="RollingTrace"
> type="log4net.Appender.RollingFileAppender">
> <param name="File" value="${ALLUSERSPROFILE}\\Trace.txt" /> <param
> name="AppendToFile" value="true" /> <param name="MaxSizeRollBackups"
> value="4" /> <param name="MaximumFileSize" value="500000" /> <param
> name="RollingStyle" value="Size" /> <param name="StaticLogFileName"
> value="true" /> <param name="Threshold" value="TRACE" /> <param
> name="PreserveLogFileNameExtension" value="true" /> <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" /> <param name="Footer" value="**** Trace Closed
> %date{yyyy-MM-dd HH:mm:ss.fff} ****%newline" /> <param
> name="ConversionPattern" value="%d{dd HH:mm:ss.fff} [%4t]
> %P{instance}::%M - %m%n" /> </layout> </appender>
>
> Just FYI, this will not work out of the box. The Header/Footer
formatting will work, but the time will not ever change with the release
code. I had to update some of the DynamicPatternLayout code to make it
re-evaluate the date/time for each file.
>
> I am willing to share the changes, but I have NO IDEA how to use the
source control product to create the changes etc.
>
>
>
>
> From: Wilco K [mailto:[email protected]]
> Sent: Thursday, October 08, 2009 09:13
> To: [email protected]
> Subject: Header in new file
>
> LS,
>
> Is it possible to log a header message to a new file (using the
> rollingfileappender)? (not a header before every log entry)
>
> Grz
> ________________________________________
> Deel je favoriete foto's online met Windows Live Photos
________________________________
Lees je Het Net, KPN/Planet, Ziggo of XS4ALL-mail in Windows Live
Hotmail
<http://www.microsoft.com/netherlands/windowslive/Views/productDetail.as
px?product=Hotmail&openVideo=5>