Example #1
0
        /// <summary>Build a JSON entry from the parameters.</summary>
        /// <remarks>Build a JSON entry from the parameters. This is public for testing.</remarks>
        /// <param name="writer">destination</param>
        /// <param name="loggerName">logger name</param>
        /// <param name="timeStamp">time_t value</param>
        /// <param name="level">level string</param>
        /// <param name="threadName">name of the thread</param>
        /// <param name="message">rendered message</param>
        /// <param name="ti">nullable thrown information</param>
        /// <returns>the writer</returns>
        /// <exception cref="System.IO.IOException">on any problem</exception>
        public virtual TextWriter ToJson(TextWriter writer, string loggerName, long timeStamp
                                         , string level, string threadName, string message, ThrowableInformation ti)
        {
            JsonGenerator json = factory.CreateJsonGenerator(writer);

            json.WriteStartObject();
            json.WriteStringField(Name, loggerName);
            json.WriteNumberField(Time, timeStamp);
            DateTime date = Extensions.CreateDate(timeStamp);

            json.WriteStringField(Date, dateFormat.Format(date));
            json.WriteStringField(Level, level);
            json.WriteStringField(Thread, threadName);
            json.WriteStringField(Message, message);
            if (ti != null)
            {
                //there is some throwable info, but if the log event has been sent over the wire,
                //there may not be a throwable inside it, just a summary.
                Exception thrown = ti.GetThrowable();
                string    eclass = (thrown != null) ? thrown.GetType().FullName : string.Empty;
                json.WriteStringField(ExceptionClass, eclass);
                string[] stackTrace = ti.GetThrowableStrRep();
                json.WriteArrayFieldStart(Stack);
                foreach (string row in stackTrace)
                {
                    json.WriteString(row);
                }
                json.WriteEndArray();
            }
            json.WriteEndObject();
            json.Flush();
            json.Close();
            return(writer);
        }
Example #2
0
 /// <exception cref="System.IO.IOException"/>
 private void WriteObject(JsonGenerator jg, object value)
 {
     if (value == null)
     {
         jg.WriteNull();
     }
     else
     {
         Type c = value.GetType();
         if (c.IsArray)
         {
             jg.WriteStartArray();
             int len = Runtime.GetArrayLength(value);
             for (int j = 0; j < len; j++)
             {
                 object item = Runtime.GetArrayValue(value, j);
                 WriteObject(jg, item);
             }
             jg.WriteEndArray();
         }
         else
         {
             if (value is Number)
             {
                 Number n = (Number)value;
                 jg.WriteNumber(n.ToString());
             }
             else
             {
                 if (value is bool)
                 {
                     bool b = (bool)value;
                     jg.WriteBoolean(b);
                 }
                 else
                 {
                     if (value is CompositeData)
                     {
                         CompositeData        cds  = (CompositeData)value;
                         CompositeType        comp = cds.GetCompositeType();
                         ICollection <string> keys = comp.KeySet();
                         jg.WriteStartObject();
                         foreach (string key in keys)
                         {
                             WriteAttribute(jg, key, cds.Get(key));
                         }
                         jg.WriteEndObject();
                     }
                     else
                     {
                         if (value is TabularData)
                         {
                             TabularData tds = (TabularData)value;
                             jg.WriteStartArray();
                             foreach (object entry in tds.Values())
                             {
                                 WriteObject(jg, entry);
                             }
                             jg.WriteEndArray();
                         }
                         else
                         {
                             jg.WriteString(value.ToString());
                         }
                     }
                 }
             }
         }
     }
 }