Table of Contents

Class TextWriterAppender

Namespace
log4net.Appender
Assembly
log4net.dll

Sends logging events to a TextWriter.

public class TextWriterAppender : AppenderSkeleton, IBulkAppender, IAppender, IOptionHandler
Inheritance
TextWriterAppender
Implements
Derived
Inherited Members

Remarks

An Appender that writes to a TextWriter.

This appender may be used stand alone if initialized with an appropriate writer, however it is typically used as a base class for an appender that can open a TextWriter to write to.

Constructors

TextWriterAppender()

Initializes a new instance of the TextWriterAppender class.

public TextWriterAppender()

Remarks

Default constructor.

TextWriterAppender(ILayout, Stream)

Initializes a new instance of the TextWriterAppender class and sets the output destination to a new StreamWriter initialized with the specified Stream.

[Obsolete("Instead use the default constructor and set the Layout & Writer properties")]
public TextWriterAppender(ILayout layout, Stream os)

Parameters

layout ILayout

The layout to use with this appender.

os Stream

The Stream to output to.

Remarks

Obsolete constructor.

TextWriterAppender(ILayout, TextWriter)

Initializes a new instance of the TextWriterAppender class and sets the output destination to the specified StreamWriter.

[Obsolete("Instead use the default constructor and set the Layout & Writer properties")]
public TextWriterAppender(ILayout layout, TextWriter writer)

Parameters

layout ILayout

The layout to use with this appender

writer TextWriter

The TextWriter to output to

Remarks

The TextWriter must have been previously opened.

Properties

ErrorHandler

Gets or set the IErrorHandler and the underlying QuietTextWriter, if any, for this appender.

public override IErrorHandler ErrorHandler { get; set; }

Property Value

IErrorHandler

The IErrorHandler for this appender.

ImmediateFlush

Gets or set whether the appender will flush at the end of each append operation.

public bool ImmediateFlush { get; set; }

Property Value

bool

The default behavior is to flush at the end of each append operation.

If this option is set to false, then the underlying stream can defer persisting the logging event to a later time.

Remarks

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.

QuietWriter

Gets or sets the QuietTextWriter where logging events will be written to.

protected QuietTextWriter QuietWriter { get; set; }

Property Value

QuietTextWriter

The QuietTextWriter where logging events are written.

Remarks

This is the QuietTextWriter where logging events will be written to.

RequiresLayout

This appender requires a log4net.Layout to be set.

protected override bool RequiresLayout { get; }

Property Value

bool

true

Remarks

This appender requires a log4net.Layout to be set.

Writer

Sets the TextWriter where the log output will go.

public virtual TextWriter Writer { get; set; }

Property Value

TextWriter

Remarks

The specified TextWriter must be open and writable.

The TextWriter will be closed when the appender instance is closed.

Note: Logging to an unopened TextWriter will fail.

Methods

Append(LoggingEvent)

This method is called by the AppenderSkeleton.DoAppend(LoggingEvent) method.

protected override void Append(LoggingEvent loggingEvent)

Parameters

loggingEvent LoggingEvent

The event to log.

Remarks

Writes a log statement to the output stream if the output stream exists and is writable.

The format of the output will depend on the appender's layout.

Append(LoggingEvent[])

This method is called by the AppenderSkeleton.DoAppend(LoggingEvent[]) method.

protected override void Append(LoggingEvent[] loggingEvents)

Parameters

loggingEvents LoggingEvent[]

The array of events to log.

Remarks

This method writes all the bulk logged events to the output writer before flushing the stream.

CloseWriter()

Closes the underlying TextWriter.

protected virtual void CloseWriter()

Remarks

Closes the underlying TextWriter.

OnClose()

Close this appender instance. The underlying stream or writer is also closed.

protected override void OnClose()

Remarks

Closed appenders cannot be reused.

PreAppendCheck()

This method determines if there is a sense in attempting to append.

protected override bool PreAppendCheck()

Returns

bool

false if any of the preconditions fail.

Remarks

This method checks if an output target has been set and if a layout has been set.

PrepareWriter()

Called to allow a subclass to lazily initialize the writer

protected virtual void PrepareWriter()

Remarks

This method is called when an event is logged and the Writer or QuietWriter have not been set. This allows a subclass to attempt to initialize the writer multiple times.

Reset()

Clears internal references to the underlying TextWriter and other variables.

protected virtual void Reset()

Remarks

Subclasses can override this method for an alternate closing behavior.

WriteFooter()

Writes a footer as produced by the embedded layout's Footer property.

protected virtual void WriteFooter()

Remarks

Writes a footer as produced by the embedded layout's Footer property.

WriteFooterAndCloseWriter()

Writes the footer and closes the underlying TextWriter.

protected virtual void WriteFooterAndCloseWriter()

Remarks

Writes the footer and closes the underlying TextWriter.

WriteHeader()

Writes a header produced by the embedded layout's Header property.

protected virtual void WriteHeader()

Remarks

Writes a header produced by the embedded layout's Header property.