You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by ni...@apache.org on 2004/11/17 22:50:18 UTC
cvs commit: logging-log4net/src/ObjectRenderer DefaultRenderer.cs
nicko 2004/11/17 13:50:18
Modified: src/ObjectRenderer DefaultRenderer.cs
Log:
Removed explicit support for rendering Exceptions. Exception.ToString gives better results.
Added support for rendering collections and dictionaries.
Revision Changes Path
1.6 +89 -128 logging-log4net/src/ObjectRenderer/DefaultRenderer.cs
Index: DefaultRenderer.cs
===================================================================
RCS file: /home/cvs/logging-log4net/src/ObjectRenderer/DefaultRenderer.cs,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- DefaultRenderer.cs 13 Sep 2004 07:05:35 -0000 1.5
+++ DefaultRenderer.cs 17 Nov 2004 21:50:18 -0000 1.6
@@ -19,65 +19,26 @@
using System;
using System.Text;
using System.IO;
+using System.Collections;
using log4net.Util;
namespace log4net.ObjectRenderer
{
/// <summary>
- /// The default Renderer renders objects by calling their <see cref="Object.ToString"/> method.
+ /// The default object Renderer.
/// </summary>
/// <remarks>
- /// <para>The default renderer supports rendering objects to strings as follows:</para>
- ///
- /// <list type="table">
- /// <listheader>
- /// <term>Value</term>
- /// <description>Rendered String</description>
- /// </listheader>
- /// <item>
- /// <term><c>null</c></term>
- /// <description><para>"(null)"</para></description>
- /// </item>
- /// <item>
- /// <term><see cref="Array"/></term>
- /// <description>
- /// <para>For a one dimensional array this is the
- /// array type name, an open brace, followed by a comma
- /// separated list of the elements (using the appropriate
- /// renderer), followed by a close brace. For example:
- /// <c>int[] {1, 2, 3}</c>.</para>
- /// <para>If the array is not one dimensional the
- /// <c>Array.ToString()</c> is returned.</para>
- /// </description>
- /// </item>
- /// <item>
- /// <term><see cref="Exception"/></term>
- /// <description>
- /// <para>Renders the exception type, message
- /// and stack trace. Any nested exception is also rendered.</para>
- /// </description>
- /// </item>
- /// <item>
- /// <term>other</term>
- /// <description>
- /// <para><c>Object.ToString()</c></para>
- /// </description>
- /// </item>
- /// </list>
- ///
- /// <para>The <see cref="DefaultRenderer"/> serves as a good base class
- /// for renderers that need to provide special handling of exception
- /// types. The <see cref="RenderException"/> method is used to render
- /// the exception and its nested exceptions, however the <see cref="RenderExceptionMessage"/>
- /// method is called just to render the exceptions message. This method
- /// can be overridden is a subclass to provide additional information
- /// for some exception types. See <see cref="RenderException"/> for
- /// more information.</para>
+ /// <para>
+ /// The default renderer supports rendering objects and collections to strings.
+ /// </para>
+ /// <para>
+ /// See the <see cref="RenderObject"/> method for details of the output.
+ /// </para>
/// </remarks>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
- public class DefaultRenderer : IObjectRenderer
+ public sealed class DefaultRenderer : IObjectRenderer
{
private static readonly string NewLine = SystemInfo.NewLine;
@@ -104,14 +65,17 @@
/// <param name="obj">The object to render</param>
/// <param name="writer">The writer to render to</param>
/// <remarks>
- /// <para>Render the object <paramref name="obj"/> to a
- /// string.</para>
+ /// <para>
+ /// Render the object <paramref name="obj"/> to a string.
+ /// </para>
///
- /// <para>The <paramref name="rendererMap"/> parameter is
+ /// <para>
+ /// The <paramref name="rendererMap"/> parameter is
/// provided to lookup and render other objects. This is
/// very useful where <paramref name="obj"/> contains
/// nested objects of unknown type. The <see cref="RendererMap.FindAndRender"/>
- /// method can be used to render these objects.</para>
+ /// method can be used to render these objects.
+ /// </para>
///
/// <para>The default renderer supports rendering objects to strings as follows:</para>
///
@@ -129,32 +93,39 @@
/// <item>
/// <term><see cref="Array"/></term>
/// <description>
- /// <para>For a one dimensional array this is the
+ /// <para>
+ /// For a one dimensional array this is the
/// array type name, an open brace, followed by a comma
/// separated list of the elements (using the appropriate
/// renderer), followed by a close brace. For example:
- /// <c>int[] {1, 2, 3}</c>.</para>
- /// <para>If the array is not one dimensional the
- /// <c>Array.ToString()</c> is returned.</para>
- ///
- /// <para>The <see cref="RenderArray"/> method is called
- /// to do the actual array rendering. This method can be
- /// overridden in a subclass to provide different array
- /// rendering.</para>
+ /// <c>int[] {1, 2, 3}</c>.
+ /// </para>
+ /// <para>
+ /// If the array is not one dimensional the
+ /// <c>Array.ToString()</c> is returned.
+ /// </para>
/// </description>
/// </item>
/// <item>
- /// <term><see cref="Exception"/></term>
+ /// <term><see cref="ICollection"/></term>
/// <description>
- /// <para>Renders the exception type, message
- /// and stack trace. Any nested exception is also rendered.</para>
- ///
- /// <para>The <see cref="RenderException"/> method is called
- /// to do the actual exception rendering. This method can be
- /// overridden in a subclass to provide different exception
- /// rendering.</para>
+ /// <para>
+ /// Rendered as an open brace, followed by a comma
+ /// separated list of the elements (using the appropriate
+ /// renderer), followed by a close brace. For example:
+ /// <c>{a, b, c}</c>.
+ /// </para>
/// </description>
- /// </item>
+ /// </item>
+ /// <item>
+ /// <term><see cref="DictionaryEntry"/></term>
+ /// <description>
+ /// <para>
+ /// Rendered as the key, an equals sign ('='), and the value (using the appropriate
+ /// renderer). For example: <c>key=value</c>.
+ /// </para>
+ /// </description>
+ /// </item>
/// <item>
/// <term>other</term>
/// <description>
@@ -163,7 +134,7 @@
/// </item>
/// </list>
/// </remarks>
- virtual public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
+ public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
{
if (rendererMap == null)
{
@@ -178,9 +149,13 @@
{
RenderArray(rendererMap, (Array)obj, writer);
}
- else if (obj is Exception)
+ else if (obj is ICollection)
{
- RenderException(rendererMap, (Exception)obj, writer);
+ RenderCollection(rendererMap, (ICollection)obj, writer);
+ }
+ else if (obj is DictionaryEntry)
+ {
+ RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
}
else
{
@@ -198,15 +173,19 @@
/// <param name="array">the array to render</param>
/// <param name="writer">The writer to render to</param>
/// <remarks>
- /// <para>For a one dimensional array this is the
+ /// <para>
+ /// For a one dimensional array this is the
/// array type name, an open brace, followed by a comma
/// separated list of the elements (using the appropriate
/// renderer), followed by a close brace. For example:
- /// <c>int[] {1, 2, 3}</c>.</para>
- /// <para>If the array is not one dimensional the
- /// <c>Array.ToString()</c> is returned.</para>
+ /// <c>int[] {1, 2, 3}</c>.
+ /// </para>
+ /// <para>
+ /// If the array is not one dimensional the
+ /// <c>Array.ToString()</c> is returned.
+ /// </para>
/// </remarks>
- virtual protected void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
+ private void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
{
if (array.Rank != 1)
{
@@ -231,76 +210,58 @@
}
/// <summary>
- /// Render the exception into a string
+ /// Render the collection argument into a string
/// </summary>
/// <param name="rendererMap">The map used to lookup renderers</param>
- /// <param name="ex">the exception to render</param>
+ /// <param name="collection">the collection to render</param>
/// <param name="writer">The writer to render to</param>
/// <remarks>
- /// <para>Renders the exception type, message, and stack trace. Any nested
- /// exceptions are also rendered.</para>
- ///
- /// <para>The <see cref="RenderExceptionMessage(RendererMap,Exception,TextWriter)"/>
- /// method is called to render the Exception's message into a string. This method
- /// can be overridden to change the behavior when rendering
- /// exceptions. To change or extend only the message that is
- /// displayed override the <see cref="RenderExceptionMessage(RendererMap,Exception,TextWriter)"/>
- /// method instead.</para>
+ /// <para>
+ /// Rendered as an open brace, followed by a comma
+ /// separated list of the elements (using the appropriate
+ /// renderer), followed by a close brace. For example:
+ /// <c>{a, b, c}</c>.
+ /// </para>
/// </remarks>
- virtual protected void RenderException(RendererMap rendererMap, Exception ex, TextWriter writer)
+ private void RenderCollection(RendererMap rendererMap, ICollection collection, TextWriter writer)
{
- writer.Write("Exception: ");
- writer.WriteLine(ex.GetType().FullName);
- writer.Write("Message: ");
- RenderExceptionMessage(rendererMap, ex, writer);
- writer.WriteLine();
+ writer.Write("{");
-#if !NETCF
- try
+ if (collection.Count > 0)
{
- if (ex.Source != null && ex.Source.Length > 0)
+ IEnumerator enumerator = collection.GetEnumerator();
+ if (enumerator != null && enumerator.MoveNext())
{
- writer.Write("Source: ");
- writer.WriteLine(ex.Source);
+ rendererMap.FindAndRender(enumerator.Current, writer);
+
+ while(enumerator.MoveNext())
+ {
+ writer.Write(", ");
+ rendererMap.FindAndRender(enumerator.Current, writer);
+ }
}
}
- catch
- {
- writer.WriteLine("Source: (Exception Occurred)");
- }
- if (ex.StackTrace != null && ex.StackTrace.Length > 0)
- {
- writer.WriteLine(ex.StackTrace);
- }
-#endif
- if (ex.InnerException != null)
- {
- writer.WriteLine();
- writer.WriteLine("Nested Exception");
- writer.WriteLine();
- RenderException(rendererMap, ex.InnerException, writer);
- writer.WriteLine();
- }
+ writer.Write("}");
}
/// <summary>
- /// Render the exception message into a string
+ /// Render the DictionaryEntry argument into a string
/// </summary>
/// <param name="rendererMap">The map used to lookup renderers</param>
- /// <param name="ex">the exception to get the message from and render</param>
+ /// <param name="entry">the DictionaryEntry to render</param>
/// <param name="writer">The writer to render to</param>
/// <remarks>
- /// <para>This method is called to render the exception's message into
- /// a string. This method should be overridden to extend the information
- /// that is rendered for a specific exception.</para>
- ///
- /// <para>See <see cref="RenderException"/> for more information.</para>
+ /// <para>
+ /// Render the key, an equals sign ('='), and the value (using the appropriate
+ /// renderer). For example: <c>key=value</c>.
+ /// </para>
/// </remarks>
- virtual protected void RenderExceptionMessage(RendererMap rendererMap, Exception ex, TextWriter writer)
+ private void RenderDictionaryEntry(RendererMap rendererMap, DictionaryEntry entry, TextWriter writer)
{
- writer.Write(ex.Message);
- }
-
+ rendererMap.FindAndRender(entry.Key, writer);
+ writer.Write("=");
+ rendererMap.FindAndRender(entry.Value, writer);
+ }
}
}