appenderskeleton.cs
来自「SharpDevelop2.0.0 c#开发免费工具」· CS 代码 · 共 709 行 · 第 1/2 页
CS
709 行
}
#endregion Implementation of IAppender
#region Public Instance Methods
/// <summary>
/// Adds a filter to the end of the filter chain.
/// </summary>
/// <param name="filter">the filter to add to this appender</param>
/// <remarks>
/// <para>
/// The Filters are organized in a linked list.
/// </para>
/// <para>
/// Setting this property causes the new filter to be pushed onto the
/// back of the filter chain.
/// </para>
/// </remarks>
virtual public void AddFilter(IFilter filter)
{
if (filter == null)
{
throw new ArgumentNullException("filter param must not be null");
}
if (m_headFilter == null)
{
m_headFilter = m_tailFilter = filter;
}
else
{
m_tailFilter.Next = filter;
m_tailFilter = filter;
}
}
/// <summary>
/// Clears the filter list for this appender.
/// </summary>
/// <remarks>
/// <para>
/// Clears the filter list for this appender.
/// </para>
/// </remarks>
virtual public void ClearFilters()
{
m_headFilter = m_tailFilter = null;
}
#endregion Public Instance Methods
#region Protected Instance Methods
/// <summary>
/// Checks if the message level is below this appender's threshold.
/// </summary>
/// <param name="level"><see cref="Level"/> to test against.</param>
/// <remarks>
/// <para>
/// If there is no threshold set, then the return value is always <c>true</c>.
/// </para>
/// </remarks>
/// <returns>
/// <c>true</c> if the <paramref name="level"/> meets the <see cref="Threshold"/>
/// requirements of this appender.
/// </returns>
virtual protected bool IsAsSevereAsThreshold(Level level)
{
return ((m_threshold == null) || level >= m_threshold);
}
/// <summary>
/// Is called when the appender is closed. Derived classes should override
/// this method if resources need to be released.
/// </summary>
/// <remarks>
/// <para>
/// Releases any resources allocated within the appender such as file handles,
/// network connections, etc.
/// </para>
/// <para>
/// It is a programming error to append to a closed appender.
/// </para>
/// </remarks>
virtual protected void OnClose()
{
// Do nothing by default
}
/// <summary>
/// Subclasses of <see cref="AppenderSkeleton"/> should implement this method
/// to perform actual logging.
/// </summary>
/// <param name="loggingEvent">The event to append.</param>
/// <remarks>
/// <para>
/// A subclass must implement this method to perform
/// logging of the <paramref name="loggingEvent"/>.
/// </para>
/// <para>This method will be called by <see cref="AppenderSkeleton.DoAppend"/>
/// if all the conditions listed for that method are met.
/// </para>
/// <para>
/// To restrict the logging of events in the appender
/// override the <see cref="PreAppendCheck()"/> method.
/// </para>
/// </remarks>
abstract protected void Append(LoggingEvent loggingEvent);
/// <summary>
/// Called before <see cref="Append"/> as a precondition.
/// </summary>
/// <remarks>
/// <para>
/// This method is called by <see cref="AppenderSkeleton.DoAppend"/>
/// before the call to the abstract <see cref="Append"/> method.
/// </para>
/// <para>
/// This method can be overridden in a subclass to extend the checks
/// made before the event is passed to the <see cref="Append"/> method.
/// </para>
/// <para>
/// A subclass should ensure that they delegate this call to
/// this base class if it is overridden.
/// </para>
/// </remarks>
/// <returns><c>true</c> if the call to <see cref="Append"/> should proceed.</returns>
virtual protected bool PreAppendCheck()
{
if ((m_layout == null) && RequiresLayout)
{
ErrorHandler.Error("AppenderSkeleton: No layout set for the appender named ["+m_name+"].");
return false;
}
return true;
}
/// <summary>
/// Renders the <see cref="LoggingEvent"/> to a string.
/// </summary>
/// <param name="loggingEvent">The event to render.</param>
/// <returns>The event rendered as a string.</returns>
/// <remarks>
/// <para>
/// Helper method to render a <see cref="LoggingEvent"/> to
/// a string. This appender must have a <see cref="Layout"/>
/// set to render the <paramref name="loggingEvent"/> to
/// a string.
/// </para>
/// <para>If there is exception data in the logging event and
/// the layout does not process the exception, this method
/// will append the exception text to the rendered string.
/// </para>
/// <para>
/// Where possible use the alternative version of this method
/// <see cref="RenderLoggingEvent(TextWriter,LoggingEvent)"/>.
/// That method streams the rendering onto an existing Writer
/// which can give better performance if the caller already has
/// a <see cref="TextWriter"/> open and ready for writing.
/// </para>
/// </remarks>
protected string RenderLoggingEvent(LoggingEvent loggingEvent)
{
// Create the render writer on first use
if (m_renderWriter == null)
{
m_renderWriter = new ReusableStringWriter(System.Globalization.CultureInfo.InvariantCulture);
}
// Reset the writer so we can reuse it
m_renderWriter.Reset(c_renderBufferMaxCapacity, c_renderBufferSize);
RenderLoggingEvent(m_renderWriter, loggingEvent);
return m_renderWriter.ToString();
}
/// <summary>
/// Renders the <see cref="LoggingEvent"/> to a string.
/// </summary>
/// <param name="loggingEvent">The event to render.</param>
/// <param name="writer">The TextWriter to write the formatted event to</param>
/// <remarks>
/// <para>
/// Helper method to render a <see cref="LoggingEvent"/> to
/// a string. This appender must have a <see cref="Layout"/>
/// set to render the <paramref name="loggingEvent"/> to
/// a string.
/// </para>
/// <para>If there is exception data in the logging event and
/// the layout does not process the exception, this method
/// will append the exception text to the rendered string.
/// </para>
/// <para>
/// Use this method in preference to <see cref="RenderLoggingEvent(LoggingEvent)"/>
/// where possible. If, however, the caller needs to render the event
/// to a string then <see cref="RenderLoggingEvent(LoggingEvent)"/> does
/// provide an efficient mechanism for doing so.
/// </para>
/// </remarks>
protected void RenderLoggingEvent(TextWriter writer, LoggingEvent loggingEvent)
{
if (m_layout == null)
{
throw new InvalidOperationException("A layout must be set");
}
if (m_layout.IgnoresException)
{
string exceptionStr = loggingEvent.GetExceptionString();
if (exceptionStr != null && exceptionStr.Length > 0)
{
// render the event and the exception
m_layout.Format(writer, loggingEvent);
writer.WriteLine(exceptionStr);
}
else
{
// there is no exception to render
m_layout.Format(writer, loggingEvent);
}
}
else
{
// The layout will render the exception
m_layout.Format(writer, loggingEvent);
}
}
/// <summary>
/// Tests if this appender requires a <see cref="Layout"/> to be set.
/// </summary>
/// <remarks>
/// <para>
/// In the rather exceptional case, where the appender
/// implementation admits a layout but can also work without it,
/// then the appender should return <c>true</c>.
/// </para>
/// <para>
/// This default implementation always returns <c>true</c>.
/// </para>
/// </remarks>
/// <returns>
/// <c>true</c> if the appender requires a layout object, otherwise <c>false</c>.
/// </returns>
virtual protected bool RequiresLayout
{
get { return false; }
}
#endregion
#region Private Instance Fields
/// <summary>
/// The layout of this appender.
/// </summary>
/// <remarks>
/// See <see cref="Layout"/> for more information.
/// </remarks>
private ILayout m_layout;
/// <summary>
/// The name of this appender.
/// </summary>
/// <remarks>
/// See <see cref="Name"/> for more information.
/// </remarks>
private string m_name;
/// <summary>
/// The level threshold of this appender.
/// </summary>
/// <remarks>
/// <para>
/// There is no level threshold filtering by default.
/// </para>
/// <para>
/// See <see cref="Threshold"/> for more information.
/// </para>
/// </remarks>
private Level m_threshold;
/// <summary>
/// It is assumed and enforced that errorHandler is never null.
/// </summary>
/// <remarks>
/// <para>
/// It is assumed and enforced that errorHandler is never null.
/// </para>
/// <para>
/// See <see cref="ErrorHandler"/> for more information.
/// </para>
/// </remarks>
private IErrorHandler m_errorHandler;
/// <summary>
/// The first filter in the filter chain.
/// </summary>
/// <remarks>
/// <para>
/// Set to <c>null</c> initially.
/// </para>
/// <para>
/// See <see cref="Filter"/> for more information.
/// </para>
/// </remarks>
private IFilter m_headFilter;
/// <summary>
/// The last filter in the filter chain.
/// </summary>
/// <remarks>
/// See <see cref="Filter"/> for more information.
/// </remarks>
private IFilter m_tailFilter;
/// <summary>
/// Flag indicating if this appender is closed.
/// </summary>
/// <remarks>
/// See <see cref="Close"/> for more information.
/// </remarks>
private bool m_closed = false;
/// <summary>
/// The guard prevents an appender from repeatedly calling its own DoAppend method
/// </summary>
private bool m_recursiveGuard = false;
/// <summary>
/// StringWriter used to render events
/// </summary>
private ReusableStringWriter m_renderWriter = null;
#endregion Private Instance Fields
#region Constants
/// <summary>
/// Initial buffer size
/// </summary>
private const int c_renderBufferSize = 256;
/// <summary>
/// Maximum buffer size before it is recycled
/// </summary>
private const int c_renderBufferMaxCapacity = 1024;
#endregion
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?