Exemplo n.º 1
0
        private void RenderExceptionData(RendererMap rendererMap, Exception ex, TextWriter writer, int depthLevel)
        {
            var dataCount = ex.Data.Count;
            if (dataCount == 0)
            {
                return;
            }

            writer.WriteLine();

            writer.WriteLine($"Exception data on level {depthLevel} ({dataCount} items):");

            var currentElement = 0;
            foreach (DictionaryEntry entry in ex.Data)
            {
                currentElement++;

                writer.Write("[");
                ExceptionObjectLogger.RenderValue(rendererMap, writer, entry.Key);
                writer.Write("]: ");

                ExceptionObjectLogger.RenderValue(rendererMap, writer, entry.Value);

                if (currentElement < dataCount)
                {
                    writer.WriteLine();
                }
            }
        }
Exemplo n.º 2
0
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     if (rendererMap == null)
     {
         throw new ArgumentNullException("rendererMap");
     }
     if (obj == null)
     {
         writer.Write(SystemInfo.NullText);
     }
     else
     {
         Array array = obj as Array;
         if (array != null)
         {
             this.RenderArray(rendererMap, array, writer);
         }
         else
         {
             IEnumerable enumerable = obj as IEnumerable;
             if (enumerable != null)
             {
                 ICollection is2 = obj as ICollection;
                 if ((is2 != null) && (is2.Count == 0))
                 {
                     writer.Write("{}");
                 }
                 else
                 {
                     IDictionary dictionary = obj as IDictionary;
                     if (dictionary != null)
                     {
                         this.RenderEnumerator(rendererMap, dictionary.GetEnumerator(), writer);
                     }
                     else
                     {
                         this.RenderEnumerator(rendererMap, enumerable.GetEnumerator(), writer);
                     }
                 }
             }
             else
             {
                 IEnumerator enumerator = obj as IEnumerator;
                 if (enumerator != null)
                 {
                     this.RenderEnumerator(rendererMap, enumerator, writer);
                 }
                 else if (obj is DictionaryEntry)
                 {
                     this.RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
                 }
                 else
                 {
                     string str = obj.ToString();
                     writer.Write((str != null) ? str : SystemInfo.NullText);
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Render the object <paramref name="obj"/> to a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <param name="obj">The object to render</param>
        /// <returns>the object rendered as a string</returns>
        /// <remarks>
        /// <para>Render the object <paramref name="obj"/> to a
        /// string.</para>
        ///
        /// <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>
        ///
        /// <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>
        ///
        ///			<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>
        ///			</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>
        ///
        ///			<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>
        ///			</description>
        ///		</item>
        ///		<item>
        ///			<term>other</term>
        ///			<description>
        ///			<para><c>Object.ToString()</c></para>
        ///			</description>
        ///		</item>
        /// </list>
        /// </remarks>
        virtual public string DoRender(RendererMap rendererMap, object obj)
        {
            if (rendererMap == null)
            {
                throw new ArgumentNullException("rendererMap");
            }

            if (obj == null)
            {
                return("(null)");
            }

            if (obj is Array)
            {
                return(RenderArray(rendererMap, (Array)obj));
            }
            else if (obj is Exception)
            {
                return(RenderException(rendererMap, (Exception)obj));
            }
            else
            {
                return(obj.ToString());
            }
        }
        protected virtual string Serialize(object obj, RendererMap map)
        {
            var type     = GetSerializedType(obj);
            var settings = GetSettings(obj, type, map);

            return(JsonConvert.SerializeObject(obj, type, settings));
        }
        /// <summary></summary>
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            if (obj == null)
            {
                return;
            }

            var objAsString = obj as string;
            if (obj is string)
            {
                writer.Write(objAsString);
                return;
            }

            if (obj is LazyString)
            {
                writer.Write(obj.ToString());
                return;
            }

            if (obj.GetType().IsTypeFromLoggingFramework())
            {
                //if we get a log4net object, log4net will expect us to call ToString to get the format
                writer.Write(obj.ToString());
                return;
            }

            obj.DumpTo(writer);
        }
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     if (obj is Amazon.CloudWatch.Model.MetricDatum)
         RenderAWSMetricDatum((Amazon.CloudWatch.Model.MetricDatum)obj, writer);
     else if (obj is MetricDatum)
         RenderAppenderMetricDatum((MetricDatum)obj, writer);
 }
        /// <summary>
        /// Write the object value as Json string into the writer using the serializer
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <param name="obj">Object to be serialized</param>
        /// <param name="writer">Will receive the serialized data of obj</param>
        public virtual void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            var serializer = Serializer ?? JsonSerializer.DefaultSerializer;
            var data       = serializer.Serialize(obj, rendererMap);

            writer.WriteLine(data);
        }
Exemplo n.º 8
0
        /// Implementation of the IObjectRenderer interface. Called on when an
        /// exception is logged.
        public void RenderObject(RendererMap map, object obj, System.IO.TextWriter writer)
        {
            Exception ex = obj as Exception;

            for (; ex != null; ex = ex.InnerException) {
                if (ex is COMException && ex.Message.StartsWith("<?xml")) {
                    writer.WriteLine();
                    writer.Write("HFMException message XML contents:");
                    writer.Write(YAML.XML.ConvertXML(ex.Message));
                }
                else {
                    writer.Write(ex.GetType().Name);
                    writer.Write(": ");
                    writer.Write(ex.Message);
                }
                if (ex.InnerException != null) {
                    writer.WriteLine();
                }
                else if (_logHierarchy.Root.Level.CompareTo(log4net.Core.Level.Fine) < 0) {
                    writer.WriteLine();
                    writer.WriteLine("Backtrace:");
                    writer.Write(ex.StackTrace);
                }
            }
        }
        /// <summary></summary>
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            if (obj == null)
            {
                return;
            }

            var objAsString = obj as string;
            if (obj is string)
            {
                writer.Write(objAsString);
                return;
            }

            if (obj is LazyString)
            {
                writer.Write(obj.ToString());
                return;
            }

            var ns = obj.GetType().Namespace;
            if (!string.IsNullOrEmpty(ns) && (ns.StartsWith("log4net") || ns.StartsWith("Common.Logging")))
            {
                //if we get a log4net object, log4net will expect us to call ToString to get the format
                writer.Write(obj.ToString());
                return;
            }

            obj.DumpTo(writer);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Serialize enumerables into a string builder
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sb"></param>
        /// <param name="map">log4net renderer map</param>
        protected virtual bool SerializeArray(IEnumerable obj, TextWriter sb, RendererMap map)
        {
            if (obj == null)
            {
                return(false);
            }

            sb.Write("[");

            bool first = true;

            foreach (var item in obj)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Write(",");
                }

                Serialize(item, sb, map, true);
            }

            sb.Write("]");

            return(true);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Serialize a dictionary into a string builder
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sb"></param>
        /// <param name="map">log4net renderer map</param>
        protected virtual bool SerializeDictionary(IDictionary obj, TextWriter sb, RendererMap map)
        {
            if (obj == null)
            {
                return(false);
            }

            sb.Write("{");

            bool first = true;

            foreach (DictionaryEntry entry in (IDictionary)obj)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.Write(",");
                }

                sb.WriteFormat(@"""{0}"":", entry.Key);

                Serialize(entry.Value, sb, map, true);
            }

            sb.Write("}");

            return(true);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Render the exception into a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <param name="ex">the exception to render</param>
        /// <returns>the string representation of the exception</returns>
        /// <remarks>
        /// <para>Renders the exception type, message, and stack trace. Any nested
        /// exceptions are also rendered.</para>
        ///
        /// <para>The <see cref="RenderExceptionMessage(RendererMap,Exception)"/>
        /// method is called to render the Exception's message into a string. This method
        /// can be overridden to change the behaviour when rendering
        /// exceptions. To change or extend only the message that is
        /// displayed override the <see cref="RenderExceptionMessage(RendererMap,Exception)"/>
        /// method instead.</para>
        /// </remarks>
        virtual protected string RenderException(RendererMap rendererMap, Exception ex)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("Exception: ")
            .Append(ex.GetType().FullName)
            .Append(NewLine)
            .Append("Message: ")
            .Append(RenderExceptionMessage(rendererMap, ex))
            .Append(NewLine);

#if !NETCF
            if (ex.Source != null && ex.Source.Length > 0)
            {
                sb.Append("Source: ").Append(ex.Source).Append(NewLine);
            }
            if (ex.StackTrace != null && ex.StackTrace.Length > 0)
            {
                sb.Append(ex.StackTrace).Append(NewLine);
            }
#endif
            if (ex.InnerException != null)
            {
                sb.Append(NewLine)
                .Append("Nested Exception")
                .Append(NewLine)
                .Append(NewLine)
                .Append(RenderException(rendererMap, ex.InnerException))
                .Append(NewLine);
            }
            return(sb.ToString());
        }
Exemplo n.º 13
0
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            var ex = obj as Exception;

            if (ex == null)
            {
                // Shouldn't happen if only configured for the System.Exception type.
                rendererMap.DefaultRenderer.RenderObject(rendererMap, obj, writer);
            }
            else
            {
                rendererMap.DefaultRenderer.RenderObject(rendererMap, obj, writer);

                const int MAX_DEPTH = 10;
                int currentDepth = 0;

                var traversedExceptions = new List<Exception>();

                while (ex != null && currentDepth <= MAX_DEPTH && !traversedExceptions.Contains(ex))
                {
                    traversedExceptions.Add(ex);

                    this.RenderExceptionData(rendererMap, ex, writer, currentDepth);
                    ex = ex.InnerException;

                    currentDepth++;
                }
            }
        }
Exemplo n.º 14
0
        public void RenderObject(RendererMap rendererMap, object obj, System.IO.TextWriter writer)
        {
            var dictionary = obj as IDictionary;
            if (dictionary == null)
                writer.Write(SystemInfo.NullText);


        }
Exemplo n.º 15
0
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     var thrown = obj as Exception;
     while (thrown != null)
     {
         RenderException(thrown, writer);
         thrown = thrown.InnerException;
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// Render the object <paramref name="obj" /> to a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <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>
        /// 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="M:RendererMap.FindAndRender(object)" />
        /// method can be used to render these objects.
        /// </para>
        /// <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="T:System.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.
        ///         </para>
        ///         <para>
        ///         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="T:System.Collections.IEnumerable" />, <see cref="T:System.Collections.ICollection" /> &amp; <see cref="T:System.Collections.IEnumerator" /></term>
        ///         <description>
        ///         <para>
        ///         Rendered as an open brace, followed by a comma
        ///         separated list of the elements (using the appropriate
        ///         renderer), followed by a close brace.
        ///         </para>
        ///         <para>
        ///         For example: <c>{a, b, c}</c>.
        ///         </para>
        ///         <para>
        ///         All collection classes that implement <see cref="T:System.Collections.ICollection" /> its subclasses,
        ///         or generic equivalents all implement the <see cref="T:System.Collections.IEnumerable" /> interface.
        ///         </para>
        ///         </description>
        ///     </item>
        ///     <item>
        ///         <term><see cref="T:System.Collections.DictionaryEntry" /></term>
        ///         <description>
        ///         <para>
        ///         Rendered as the key, an equals sign ('='), and the value (using the appropriate
        ///         renderer).
        ///         </para>
        ///         <para>
        ///         For example: <c>key=value</c>.
        ///         </para>
        ///         </description>
        ///     </item>
        ///     <item>
        ///         <term>other</term>
        ///         <description>
        ///         <para><c>Object.ToString()</c></para>
        ///         </description>
        ///     </item>
        /// </list>
        /// </remarks>
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            if (rendererMap == null)
            {
                throw new ArgumentNullException("rendererMap");
            }
            if (obj == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }
            Array array = obj as Array;

            if (array != null)
            {
                RenderArray(rendererMap, array, writer);
                return;
            }
            IEnumerable enumerable = obj as IEnumerable;

            if (enumerable != null)
            {
                ICollection collection = obj as ICollection;
                if (collection != null && collection.Count == 0)
                {
                    writer.Write("{}");
                    return;
                }
                IDictionary dictionary = obj as IDictionary;
                if (dictionary != null)
                {
                    RenderEnumerator(rendererMap, dictionary.GetEnumerator(), writer);
                }
                else
                {
                    RenderEnumerator(rendererMap, enumerable.GetEnumerator(), writer);
                }
            }
            else
            {
                IEnumerator enumerator = obj as IEnumerator;
                if (enumerator != null)
                {
                    RenderEnumerator(rendererMap, enumerator, writer);
                    return;
                }
                if (obj is DictionaryEntry)
                {
                    RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
                    return;
                }
                string text = obj.ToString();
                writer.Write((text == null) ? SystemInfo.NullText : text);
            }
        }
Exemplo n.º 17
0
 protected LoggerRepositorySkeleton(PropertiesDictionary properties)
 {
     this.m_properties            = properties;
     this.m_rendererMap           = new log4net.ObjectRenderer.RendererMap();
     this.m_pluginMap             = new log4net.Plugin.PluginMap(this);
     this.m_levelMap              = new log4net.Core.LevelMap();
     this.m_configurationMessages = EmptyCollection.Instance;
     this.m_configured            = false;
     this.AddBuiltinLevels();
     this.m_threshold = Level.All;
 }
Exemplo n.º 18
0
 private static void RenderValue(RendererMap rendererMap, TextWriter writer, object value)
 {
     if (value is string)
     {
         writer.Write(value);
     }
     else
     {
         IObjectRenderer keyRenderer = rendererMap.Get(value.GetType());
         keyRenderer.RenderObject(rendererMap, value, writer);
     }
 }
Exemplo n.º 19
0
    /// <summary>
    /// Construct the repository using specific properties
    /// </summary>
    /// <param name="properties">the properties to set for this repository</param>
    /// <remarks>
    /// <para>
    /// Initializes the repository with specified properties.
    /// </para>
    /// </remarks>
    protected LoggerRepositorySkeleton(PropertiesDictionary properties) {
      m_properties = properties;
      m_rendererMap = new RendererMap();
      m_pluginMap = new PluginMap(this);
      m_levelMap = new LevelMap();
      m_configured = false;

      AddBuiltinLevels();

      // Don't disable any levels by default.
      m_threshold = Level.All;
    }
Exemplo n.º 20
0
 /// <summary>
 /// Render the enumerator argument into a string
 /// </summary>
 /// <param name="rendererMap">The map used to lookup renderers</param>
 /// <param name="enumerator">the enumerator to render</param>
 /// <param name="writer">The writer to render to</param>
 /// <remarks>
 /// <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>
 private void RenderEnumerator(RendererMap rendererMap, IEnumerator enumerator, TextWriter writer)
 {
     writer.Write("{");
     if (enumerator != null && enumerator.MoveNext())
     {
         rendererMap.FindAndRender(enumerator.Current, writer);
         while (enumerator.MoveNext())
         {
             writer.Write(", ");
             rendererMap.FindAndRender(enumerator.Current, writer);
         }
     }
     writer.Write("}");
 }
Exemplo n.º 21
0
		public void RenderObject(RendererMap rendererMap, object obj, System.IO.TextWriter writer)
		{
			var dictionary = obj as IDictionary;
			var jsonSerializerSettings = new JsonSerializerSettings
			{
				ContractResolver = new CamelCasePropertyNamesContractResolver()
			};
			var serializedLog = JsonConvert.SerializeObject(dictionary, Formatting.None, jsonSerializerSettings);
			if (dictionary == null)
			{
				writer.Write(SystemInfo.NullText);
			}
			else
			{
				writer.Write(serializedLog);
			}
		}
Exemplo n.º 22
0
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            var ex = obj as Exception;

            //if its not an exception, dump it. If it's an exception, log extended details.
            if (ex == null)
            {
                //by default log up to 10 levels deep.
                ObjectDumper.Write(obj, 10, writer);
            }
            else
            {
                while (ex != null)
                {
                    RenderException(ex, writer);
                    ex = ex.InnerException;
                }
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Render the array argument into a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <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
        /// 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>
        /// </remarks>
        private void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
        {
            if (array.Rank != 1)
            {
                writer.Write(array.ToString());
                return;
            }
            writer.Write(array.GetType().Name + " {");
            int length = array.Length;

            if (length > 0)
            {
                rendererMap.FindAndRender(array.GetValue(0), writer);
                for (int i = 1; i < length; i++)
                {
                    writer.Write(", ");
                    rendererMap.FindAndRender(array.GetValue(i), writer);
                }
            }
            writer.Write("}");
        }
Exemplo n.º 24
0
        /// <summary>
        /// Render the array argument into a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <param name="array">the array to render</param>
        /// <returns>the string representation of the array</returns>
        /// <remarks>
        /// <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>
        /// </remarks>
        virtual protected string RenderArray(RendererMap rendererMap, Array array)
        {
            if (array.Rank != 1)
            {
                return(array.ToString());
            }
            else
            {
                StringBuilder buffer = new StringBuilder(array.GetType().Name + " {");
                int           len    = array.Length;

                if (len > 0)
                {
                    buffer.Append(rendererMap.FindAndRender(array.GetValue(0)));
                    for (int i = 1; i < len; i++)
                    {
                        buffer.Append(", ").Append(rendererMap.FindAndRender(array.GetValue(i)));
                    }
                }
                return(buffer.Append("}").ToString());
            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// Render the array argument into a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <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
        ///	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>
        /// </remarks>
        void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
        {
            if (array.Rank != 1)
            {
                writer.Write(array.ToString());
            }
            else
            {
                writer.Write(array.GetType().Name + " {");
                var len = array.Length;

                if (len > 0)
                {
                    rendererMap.FindAndRender(array.GetValue(0), writer);
                    for (var i = 1; i < len; i++)
                    {
                        writer.Write(", ");
                        rendererMap.FindAndRender(array.GetValue(i), writer);
                    }
                }
                writer.Write("}");
            }
        }
Exemplo n.º 26
0
        /// <summary>
        /// Serialize any object into a string builder
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sb"></param>
        /// <param name="map">log4net renderer map</param>
        protected virtual void Serialize(object obj, TextWriter sb, RendererMap map, bool tryMapFirst)
        {
            var serialized = (tryMapFirst && SerializeObjectUsingRendererMap(obj, sb, map)) ||
                             SerializeNull(obj, sb) || // null gate first, others do not expect nulls
                             SerializeDictionary(obj as IDictionary, sb, map) ||
                             SerializeString(obj as string, sb) ||
                             SerializeChars(obj as char[], sb) ||
                             SerializeBytes(obj as byte[], sb) ||
                             SerializeDateTime(obj, sb) ||
                             SerializeTimeSpan(obj, sb) ||
                             SerializePrimitive(obj, sb) ||
                             SerializeEnum(obj, sb) ||
                             SerializeGuid(obj, sb) ||
                             SerializeUri(obj as Uri, sb) ||
                             SerializeArray(obj as IEnumerable, sb, map) || // goes almost last not to interfere with string, char[], byte[]...
                             SerializeObjectAsDictionary(obj, sb, map) // before last resort
            ;

            if (!serialized)
            {
                SerializeString(Convert.ToString(obj), sb); // last resort
            }
        }
Exemplo n.º 27
0
        // $PSRenderer = new-object PSLog4NET.PSObjectRenderer
        // $Repository = [log4net.LogManager]::GetRepository()
        // $Repository.RendererMap.Put([PSLog4NET.PSObjectRenderer],(new-object PSLog4NET.PSObjectRenderer) )
        //  # rver -> http://blogs.msdn.com/powershell/archive/2006/12/07/resolve-error.aspx
        // $PSRenderer|Add-Member -Force -MemberType ScriptMethod ToString { rver|Out-String }
        // trap {$Logger.Error($PSRenderer)}
        public void RenderObject(log4net.ObjectRenderer.RendererMap rendererMap, object obj, System.IO.TextWriter writer)
        {
            if (rendererMap == null)
            {
                throw new ArgumentNullException("rendererMap");
            }

            if (obj == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }
            PSObjectRenderer Instance = obj as PSObjectRenderer;

            if (Instance != null)
            //On laisse le scripteur redéclarer la méthode ToString à l'aide de Add-Member
            {
                writer.Write("{0}", Instance.ToString());
            }
            else
            {
                writer.Write(SystemInfo.NullText);
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// Serialize an object as a dictionary (last resort) into a string builder
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sb"></param>
        /// <param name="map">log4net renderer map</param>
        protected virtual bool SerializeObjectAsDictionary(Object obj, TextWriter sb, RendererMap map)
        {
            if (obj == null)
            {
                return(false);
            }

            var dict = ObjToDict(obj, SaveType, SaveInternalType, TypeMemberName, Stringify, StringMemberName);

            SerializeDictionary(dict, sb, map);

            return(true);
        }
Exemplo n.º 29
0
 /// <summary>
 /// Renders the object as JSON.
 /// </summary>
 /// <param name="rendererMap">Renderer map for rendering overrides.</param>
 /// <param name="obj">Object to be serialized.</param>
 /// <param name="writer">Writer to write to.</param>
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     Serialize(obj, writer, rendererMap, false);
 }
Exemplo n.º 30
0
		/// <summary>
		/// Render the collection argument into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="collection">the collection to render</param>
		/// <param name="writer">The writer to render to</param>
		/// <remarks>
		/// <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>
		private void RenderCollection(RendererMap rendererMap, ICollection collection, TextWriter writer)
		{
			writer.Write("{");

			if (collection.Count > 0)
			{
				IEnumerator enumerator = collection.GetEnumerator();
				if (enumerator != null && enumerator.MoveNext())
				{
					rendererMap.FindAndRender(enumerator.Current, writer);

					while(enumerator.MoveNext())
					{
						writer.Write(", ");
						rendererMap.FindAndRender(enumerator.Current, writer);
					}
				}
			}

			writer.Write("}");
		}
		/// <summary>
		/// Render the exception message 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>
		/// <returns>the string representation of the exception message</returns>
		/// <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>
		/// </remarks>
		virtual protected string RenderExceptionMessage(RendererMap rendererMap, Exception ex)
		{
			return ex.Message;
		}
Exemplo n.º 32
0
		/// <summary>
		/// Render the array argument into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <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
		///	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>
		/// </remarks>
		private void RenderArray(RendererMap rendererMap, Array array, TextWriter writer)
		{
			if (array.Rank != 1)
			{
				writer.Write(array.ToString());
			}
			else
			{
				writer.Write(array.GetType().Name + " {");
				int len = array.Length;

				if (len > 0)
				{
					rendererMap.FindAndRender(array.GetValue(0), writer);
					for(int i=1; i<len; i++)
					{
						writer.Write(", ");
						rendererMap.FindAndRender(array.GetValue(i), writer);
					}
				}
				writer.Write("}");
			}
		}
Exemplo n.º 33
0
		/// <summary>
		/// Render the DictionaryEntry argument into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="entry">the DictionaryEntry to render</param>
		/// <param name="writer">The writer to render to</param>
		/// <remarks>
		/// <para>
		/// Render the key, an equals sign ('='), and the value (using the appropriate
		///	renderer). For example: <c>key=value</c>.
		///	</para>
		/// </remarks>
		private void RenderDictionaryEntry(RendererMap rendererMap, DictionaryEntry entry, TextWriter writer)
		{
			rendererMap.FindAndRender(entry.Key, writer);
			writer.Write("=");
			rendererMap.FindAndRender(entry.Value, writer);
		}	
Exemplo n.º 34
0
 public void RenderObject(RendererMap a, object b, TextWriter c)
 {
 }
Exemplo n.º 35
0
 /// <summary>
 /// Render the DictionaryEntry argument into a string
 /// </summary>
 /// <param name="rendererMap">The map used to lookup renderers</param>
 /// <param name="entry">the DictionaryEntry to render</param>
 /// <param name="writer">The writer to render to</param>
 /// <remarks>
 /// <para>
 /// Render the key, an equals sign ('='), and the value (using the appropriate
 ///     renderer). For example: <c>key=value</c>.
 ///     </para>
 /// </remarks>
 private void RenderDictionaryEntry(RendererMap rendererMap, DictionaryEntry entry, TextWriter writer)
 {
     rendererMap.FindAndRender(entry.Key, writer);
     writer.Write("=");
     rendererMap.FindAndRender(entry.Value, writer);
 }
Exemplo n.º 36
0
 /// <summary>
 /// Render the exception message 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>
 /// <returns>the string representation of the exception message</returns>
 /// <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>
 /// </remarks>
 virtual protected string RenderExceptionMessage(RendererMap rendererMap, Exception ex)
 {
     return(ex.Message);
 }
Exemplo n.º 37
0
 static RendererMap()
 {
     _instance = new RendererMap();
 }
Exemplo n.º 38
0
 /// <summary>
 /// Get Json.NET settings
 /// </summary>
 /// <remarks>
 /// This object itself is the settings. RendererMap is not supported.
 /// </remarks>
 /// <param name="obj">Value to be serialized</param>
 /// <param name="type">Type to serialize as</param>
 /// <param name="map">Renderer map</param>
 /// <returns>Settings for this call</returns>
 protected virtual JsonSerializerSettings GetSettings(object obj, Type type, RendererMap map)
 => this;
Exemplo n.º 39
0
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     if (obj is SQSDatum)
         RenderAppenderLogDatum((SQSDatum)obj, writer);
 }
Exemplo n.º 40
0
        /// <summary>
        /// Serialize value into text writer
        /// </summary>
        /// <remarks>
        /// I've tried and failed to support the RendereMap here in some
        /// decent way. Pull requests welcome. Until then, map is ignored.
        /// </remarks>
        /// <param name="rendererMap">Renderer map - ignored</param>
        /// <param name="value">Value to be serialized</param>
        /// <param name="writer">Where JSON will be written</param>
        public void RenderObject(RendererMap rendererMap, object value, TextWriter writer)
        {
            var json = Serialize(value, rendererMap);

            writer.Write(json);
        }
Exemplo n.º 41
0
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
 }
Exemplo n.º 42
0
        /// <summary>
        /// Serialize an object into a string builder using another serializer registered in the renderer map
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="sb"></param>
        /// <param name="map">log4net renderer map</param>
        protected virtual bool SerializeObjectUsingRendererMap(Object obj, TextWriter sb, RendererMap map)
        {
            if (obj == null)
            {
                return(false);
            }
            if (map == null)
            {
                return(false);
            }

            var customSerializer = map.Get(obj) as IJsonRenderer;

            if (customSerializer == null)
            {
                return(false);
            }

            customSerializer.RenderObject(map, obj, sb);

            return(true);
        }
Exemplo n.º 43
0
        /// <summary>
        /// Render the object <paramref name="obj"/> to a string
        /// </summary>
        /// <param name="rendererMap">The map used to lookup renderers</param>
        /// <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>
        /// 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(object)"/>
        /// method can be used to render these objects.
        /// </para>
        /// <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.
        ///             </para>
        ///             <para>
        ///             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="IEnumerable"/>, <see cref="ICollection"/> &amp; <see cref="IEnumerator"/></term>
        ///             <description>
        ///             <para>
        ///             Rendered as an open brace, followed by a comma
        ///             separated list of the elements (using the appropriate
        ///             renderer), followed by a close brace.
        ///             </para>
        ///             <para>
        ///             For example: <c>{a, b, c}</c>.
        ///             </para>
        ///             <para>
        ///             All collection classes that implement <see cref="ICollection"/> its subclasses,
        ///             or generic equivalents all implement the <see cref="IEnumerable"/> interface.
        ///             </para>
        ///             </description>
        ///         </item>
        ///         <item>
        ///             <term><see cref="DictionaryEntry"/></term>
        ///             <description>
        ///             <para>
        ///             Rendered as the key, an equals sign ('='), and the value (using the appropriate
        ///             renderer).
        ///             </para>
        ///             <para>
        ///             For example: <c>key=value</c>.
        ///             </para>
        ///             </description>
        ///         </item>
        ///         <item>
        ///             <term>other</term>
        ///             <description>
        ///             <para><c>Object.ToString()</c></para>
        ///             </description>
        ///         </item>
        /// </list>
        /// </remarks>
        public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
        {
            if (rendererMap == null)
            {
                throw new ArgumentNullException("rendererMap");
            }

            if (obj == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }

            Array objArray = obj as Array;

            if (objArray != null)
            {
                RenderArray(rendererMap, objArray, writer);
                return;
            }

            // Test if we are dealing with some form of collection object
            IEnumerable objEnumerable = obj as IEnumerable;

            if (objEnumerable != null)
            {
                // Get a collection interface if we can as its .Count property may be more
                // performant than getting the IEnumerator object and trying to advance it.
                ICollection objCollection = obj as ICollection;
                if (objCollection != null && objCollection.Count == 0)
                {
                    writer.Write("{}");
                    return;
                }

                // This is a special check to allow us to get the enumerator from the IDictionary
                // interface as this guarantees us DictionaryEntry objects. Note that in .NET 2.0
                // the generic IDictionary<> interface enumerates KeyValuePair objects rather than
                // DictionaryEntry ones. However the implementation of the plain IDictionary
                // interface on the generic Dictionary<> still returns DictionaryEntry objects.
                IDictionary objDictionary = obj as IDictionary;
                if (objDictionary != null)
                {
                    RenderEnumerator(rendererMap, objDictionary.GetEnumerator(), writer);
                    return;
                }

                RenderEnumerator(rendererMap, objEnumerable.GetEnumerator(), writer);
                return;
            }

            IEnumerator objEnumerator = obj as IEnumerator;

            if (objEnumerator != null)
            {
                RenderEnumerator(rendererMap, objEnumerator, writer);
                return;
            }

            if (obj is DictionaryEntry)
            {
                RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
                return;
            }

            string str = obj.ToString();

            writer.Write((str == null) ? SystemInfo.NullText : str);
        }
		/// <summary>
		/// Render the object <paramref name="obj"/> to a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="obj">The object to render</param>
		/// <returns>the object rendered as a string</returns>
		/// <remarks>
		/// <para>Render the object <paramref name="obj"/> to a 
		/// string.</para>
		/// 
		/// <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>
		/// 
		/// <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>
		///			
		///			<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>
		///			</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>
		///			
		///			<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>
		///			</description>
		///		</item>
		///		<item>
		///			<term>other</term>
		///			<description>
		///			<para><c>Object.ToString()</c></para>
		///			</description>
		///		</item>
		/// </list>
		/// </remarks>
		virtual public string DoRender(RendererMap rendererMap, object obj) 
		{
			if (rendererMap == null)
			{
				throw new ArgumentNullException("rendererMap");
			}

			if (obj == null)
			{
				return "(null)";
			}

			if (obj is Array)
			{
				return RenderArray(rendererMap, (Array)obj);
			}
			else if (obj is Exception)
			{
				return RenderException(rendererMap, (Exception)obj);
			}
			else
			{
				return obj.ToString();
			}
		}
Exemplo n.º 45
0
		/// <summary>
		/// Render the object <paramref name="obj"/> to a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <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>
		/// 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(object)"/>
		/// method can be used to render these objects.
		/// </para>
		/// <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. 
		///			</para>
		///			<para>
		///			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="IEnumerable"/>, <see cref="ICollection"/> &amp; <see cref="IEnumerator"/></term>
		///			<description>
		///			<para>
		///			Rendered as an open brace, followed by a comma
		///			separated list of the elements (using the appropriate
		///			renderer), followed by a close brace.
		///			</para>
		///			<para>
		///			For example: <c>{a, b, c}</c>.
		///			</para>
		///			<para>
		///			All collection classes that implement <see cref="ICollection"/> its subclasses, 
		///			or generic equivalents all implement the <see cref="IEnumerable"/> interface.
		///			</para>
		///			</description>
		///		</item>		
		///		<item>
		///			<term><see cref="DictionaryEntry"/></term>
		///			<description>
		///			<para>
		///			Rendered as the key, an equals sign ('='), and the value (using the appropriate
		///			renderer). 
		///			</para>
		///			<para>
		///			For example: <c>key=value</c>.
		///			</para>
		///			</description>
		///		</item>		
		///		<item>
		///			<term>other</term>
		///			<description>
		///			<para><c>Object.ToString()</c></para>
		///			</description>
		///		</item>
		/// </list>
		/// </remarks>
		public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
		{
			if (rendererMap == null)
			{
				throw new ArgumentNullException("rendererMap");
			}

			if (obj == null)
			{
				writer.Write(SystemInfo.NullText);
				return;
			}
			
			Array objArray = obj as Array;
			if (objArray != null)
			{
				RenderArray(rendererMap, objArray, writer);
				return;
			}

			// Test if we are dealing with some form of collection object
			IEnumerable objEnumerable = obj as IEnumerable;
			if (objEnumerable != null)
			{
				// Get a collection interface if we can as its .Count property may be more
				// performant than getting the IEnumerator object and trying to advance it.
				ICollection objCollection = obj as ICollection;
				if (objCollection != null && objCollection.Count == 0)
				{
					writer.Write("{}");
					return;
				}
				
				// This is a special check to allow us to get the enumerator from the IDictionary
				// interface as this guarantees us DictionaryEntry objects. Note that in .NET 2.0
				// the generic IDictionary<> interface enumerates KeyValuePair objects rather than
				// DictionaryEntry ones. However the implementation of the plain IDictionary 
				// interface on the generic Dictionary<> still returns DictionaryEntry objects.
				IDictionary objDictionary = obj as IDictionary;
				if (objDictionary != null)
				{
					RenderEnumerator(rendererMap, objDictionary.GetEnumerator(), writer);
					return;
				}

				RenderEnumerator(rendererMap, objEnumerable.GetEnumerator(), writer);
				return;
			}

			IEnumerator objEnumerator = obj as IEnumerator;
			if (objEnumerator != null)
			{
				RenderEnumerator(rendererMap, objEnumerator, writer);
				return;
			}
			
			if (obj is DictionaryEntry)
			{
				RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
				return;
			}

			string str = obj.ToString();
			writer.Write( (str==null) ? SystemInfo.NullText : str );
		}
		/// <summary>
		/// Render the array argument into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="array">the array to render</param>
		/// <returns>the string representation of the array</returns>
		/// <remarks>
		/// <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>
		/// </remarks>
		virtual protected string RenderArray(RendererMap rendererMap, Array array)
		{
			if (array.Rank != 1)
			{
				return array.ToString();
			}
			else
			{
				StringBuilder buffer = new StringBuilder(array.GetType().Name + " {");
				int len = array.Length;

				if (len > 0)
				{
					buffer.Append(rendererMap.FindAndRender(array.GetValue(0)));
					for(int i=1; i<len; i++)
					{
						buffer.Append(", ").Append(rendererMap.FindAndRender(array.GetValue(i)));
					}
				}
				return buffer.Append("}").ToString();
			}
		}
Exemplo n.º 47
0
		/// <summary>
		/// Render the enumerator argument into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="enumerator">the enumerator to render</param>
		/// <param name="writer">The writer to render to</param>
		/// <remarks>
		/// <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>
		private void RenderEnumerator(RendererMap rendererMap, IEnumerator enumerator, TextWriter writer)
		{
			writer.Write("{");

			if (enumerator != null && enumerator.MoveNext())
			{
				rendererMap.FindAndRender(enumerator.Current, writer);

				while (enumerator.MoveNext())
				{
					writer.Write(", ");
					rendererMap.FindAndRender(enumerator.Current, writer);
				}
			}

			writer.Write("}");
		}
		/// <summary>
		/// Render the exception into a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <param name="ex">the exception to render</param>
		/// <returns>the string representation of the exception</returns>
		/// <remarks>
		/// <para>Renders the exception type, message, and stack trace. Any nested
		/// exceptions are also rendered.</para>
		/// 
		/// <para>The <see cref="RenderExceptionMessage(RendererMap,Exception)"/>
		/// method is called to render the Exception's message into a string. This method
		/// can be overridden to change the behaviour when rendering
		/// exceptions. To change or extend only the message that is
		/// displayed override the <see cref="RenderExceptionMessage(RendererMap,Exception)"/>
		/// method instead.</para>
		/// </remarks>
		virtual protected string RenderException(RendererMap rendererMap, Exception ex)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			sb.Append("Exception: ")
				.Append(ex.GetType().FullName)
				.Append(NewLine)
				.Append("Message: ")
				.Append(RenderExceptionMessage(rendererMap, ex))
				.Append(NewLine);

#if !NETCF
			if (ex.Source != null && ex.Source.Length > 0)
			{
				sb.Append("Source: ").Append(ex.Source).Append(NewLine);
			}
			if (ex.StackTrace != null && ex.StackTrace.Length > 0)
			{
				sb.Append(ex.StackTrace).Append(NewLine);
			}
#endif
			if (ex.InnerException != null)
			{
				sb.Append(NewLine)
					.Append("Nested Exception")
					.Append(NewLine)
					.Append(NewLine)
					.Append(RenderException(rendererMap, ex.InnerException))
					.Append(NewLine);
			}
			return sb.ToString();
		}
 /// <summary>
 /// Renderiza una excepción como JSON.
 /// </summary>
 /// <param name="rendererMap">The renderMap.</param>
 /// <param name="obj">Excepción a renderizar como JSON.</param>
 /// <param name="writer">The writer.</param>
 public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
 {
     writer.Write((obj as Exception).ToJson());
 }
Exemplo n.º 50
0
		/// <summary>
		/// Render the object <paramref name="obj"/> to a string
		/// </summary>
		/// <param name="rendererMap">The map used to lookup renderers</param>
		/// <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>
		/// 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>
		/// <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="ICollection"/></term>
		///			<description>
		///			<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>
		///			<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>
		///			<para><c>Object.ToString()</c></para>
		///			</description>
		///		</item>
		/// </list>
		/// </remarks>
		public void RenderObject(RendererMap rendererMap, object obj, TextWriter writer)
		{
			if (rendererMap == null)
			{
				throw new ArgumentNullException("rendererMap");
			}

			if (obj == null)
			{
				writer.Write("(null)");
				return;
			}
			
			Array objArray = obj as Array;
			if (objArray != null)
			{
				RenderArray(rendererMap, objArray, writer);
				return;
			}
			
			ICollection objCollection = obj as ICollection;
			if (objCollection != null)
			{
				RenderCollection(rendererMap, objCollection, writer);
				return;
			}
			
			if (obj is DictionaryEntry)
			{
				RenderDictionaryEntry(rendererMap, (DictionaryEntry)obj, writer);
				return;
			}

			string str = obj.ToString();
			writer.Write( (str==null) ? "(null)" : str );
		}