Hallo everyone,
I have implemented a really simple event appender, which allows me to
forward the message from the Log4Net Framework in my own application.
This also works fine, but the layout settings from the configuration
file are not applied to my appender. The same Layout settings work fine
for the default appenders, which I removed for simplification. I added
the appender source code and config below. Hopefully somebody can tell
me how to fix this problem.
Thx in advance,
Theo Jungeblut
Settings:
------------------------------
Log4Net Version 1.2.9 beta
OS: Win XP SP2
.Net: 1.1 (VS2003)
Assembly type: DLL
Application type: windows application
Appender Source code:
------------------------------------------------------------------------
-----------------
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using MyEvents;
namespace MyAppender
{
/// <summary>
/// Adapts the Log4Net message for inner application usage.
/// When configured in the Log4Net configuration, each log
message
/// results into an OnMessageSent event.
/// </summary>
public class EventAppender: AppenderSkeleton
{
#region --------------------- Private Fields
---------------------
/// <summary>
/// Event raised when message is send
/// </summary>
public event MessageSentEventHandler MessageSent;
#endregion
#region --------------------- Private Fields
---------------------
/// <summary>
/// Immediate flush means that the underlying writer or
output stream
/// will be flushed at the end of each append operation.
/// </summary>
/// <remarks>
/// <para>
/// Immediate flush is slower but ensures that each
append request is
/// actually written. If <see cref="ImmediateFlush"/> is
set to
/// <c>false</c>, then there is a good chance that the
last few
/// logs events are not actually written to persistent
media if and
/// when the application crashes.
/// </para>
/// <para>
/// The default value is <c>true</c>.</para>
/// </remarks>
private bool immediateFlush;
#endregion
#region --------------------- Public Properties
---------------------
#region ImmediateFlush
/// <summary>
/// Gets or sets a value that indicates whether the
appender will
/// flush at the end of each write.
/// </summary>
/// <remarks>
/// <para>The default behavior is to flush at the end of
each
/// write. If the option is set to<c>false</c>, then the
underlying
/// stream can defer writing to physical medium to a
later time.
/// </para>
/// <para>
/// Avoiding the flush operation at the end of each
append results
/// in a performance gain of 10 to 20 percent. However,
there is safety
/// trade-off involved in skipping flushing. Indeed,
when flushing is
/// skipped, then it is likely that the last few log
events will not
/// be recorded on disk when the application exits. This
is a high
/// price to pay even for a 20% performance gain.
/// </para>
/// </remarks>
public bool ImmediateFlush
{
get
{
return immediateFlush;
}
set
{
immediateFlush = value;
}
}
#endregion
#region RequiresLayout
/// <summary>
/// This appender requires a <see cref="Layout"/> to be
set.
/// </summary>
/// <value><c>true</c></value>
/// <remarks>
/// <para>
/// This appender requires a <see cref="Layout"/> to be
set.
/// </para>
/// </remarks>
override protected bool RequiresLayout
{
get { return true; }
}
#endregion
/// <summary>
/// Gets and sets the loqout for the appender
/// </summary>
public override ILayout Layout
{
get
{
return base.Layout;
}
set
{
base.Layout = value;
}
}
#endregion
#region --------------------- Constructor/Destructor
---------------------
/// <summary>
/// Initializes a new instance of the <see
cref="EventAppender" />.
/// </summary>
/// <remarks>
/// <para>
/// Default constructor.
/// </para>
/// </remarks>
public EventAppender()
{
this.immediateFlush = true;
}
/// <summary>
/// Initializes a new instance of the <see
cref="TraceAppender" />
/// with a specified layout.
/// </summary>
/// <param name="layout">The layout to use with this
appender.</param>
/// <remarks>
/// <para>
/// Obsolete constructor.
/// </para>
/// </remarks>
[System.Obsolete("Instead use the default constructor
and set the Layout property")]
public EventAppender(ILayout layout):this()
{
Layout = layout;
}
#endregion
#region --------------------- Eventhandler
---------------------
#region Append
/// <summary>
/// Writes the logging event to the <see
cref="System.Diagnostics.Trace"/> system.
/// </summary>
/// <param name="loggingEvent">The event to log.</param>
/// <remarks>
/// <para>
/// Writes the logging event to the <see
cref="System.Diagnostics.Trace"/> system.
/// </para>
/// </remarks>
override protected void Append(LoggingEvent
loggingEvent)
{
OnMessageSent(new MessageSentEventArgs(
loggingEvent ) );
}
#endregion
#region OnMessageSent
/// <summary>
///
/// </summary>
/// <param name="arg"></param>
protected virtual void
OnMessageSent(MessageSentEventArgs arg)
{
if (MessageSent != null)
{
MessageSent(this, arg);
}
}
#endregion
#endregion
}
}
Config File:
------------------------------------------------------------------------
-------------------
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="Event" type="Ade.Fdt.Container.EventAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date - %message
%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="Event" />
</root>
</log4net>