Ejemplo n.º 1
0
        public virtual void TestNestedException()
        {
            Exception            e   = new NoRouteToHostException("that box caught fire 3 years ago");
            Exception            ioe = new IOException("Datacenter problems", e);
            ThrowableInformation ti  = new ThrowableInformation(ioe);
            Log4Json             l4j = new Log4Json();
            long   timeStamp         = Time.Now();
            string outcome           = l4j.ToJson(new StringWriter(), "testNestedException", timeStamp,
                                                  "INFO", "quoted\"", "new line\n and {}", ti).ToString();

            Println("testNestedException", outcome);
            ContainerNode rootNode = Log4Json.Parse(outcome);

            AssertEntryEquals(rootNode, Log4Json.Level, "INFO");
            AssertEntryEquals(rootNode, Log4Json.Name, "testNestedException");
            AssertEntryEquals(rootNode, Log4Json.Time, timeStamp);
            AssertEntryEquals(rootNode, Log4Json.ExceptionClass, ioe.GetType().FullName);
            JsonNode node = AssertNodeContains(rootNode, Log4Json.Stack);

            Assert.True("Not an array: " + node, node.IsArray());
            node = AssertNodeContains(rootNode, Log4Json.Date);
            Assert.True("Not a string: " + node, node.IsTextual());
            //rather than try and make assertions about the format of the text
            //message equalling another ISO date, this test asserts that the hypen
            //and colon characters are in the string.
            string dateText = node.GetTextValue();

            Assert.True("No '-' in " + dateText, dateText.Contains("-"));
            Assert.True("No '-' in " + dateText, dateText.Contains(":"));
        }
Ejemplo n.º 2
0
        /// <summary>Convert an event to JSON</summary>
        /// <param name="writer">the destination writer</param>
        /// <param name="event">the event -must not be null</param>
        /// <returns>the writer</returns>
        /// <exception cref="System.IO.IOException">on problems generating the JSON</exception>
        public virtual TextWriter ToJson(TextWriter writer, LoggingEvent @event)
        {
            ThrowableInformation ti = @event.GetThrowableInformation();

            ToJson(writer, @event.GetLoggerName(), @event.GetTimeStamp(), @event.GetLevel().ToString
                       (), @event.GetThreadName(), @event.GetRenderedMessage(), ti);
            return(writer);
        }
Ejemplo n.º 3
0
        public virtual void TestException()
        {
            Exception            e   = new NoRouteToHostException("that box caught fire 3 years ago");
            ThrowableInformation ti  = new ThrowableInformation(e);
            Log4Json             l4j = new Log4Json();
            long   timeStamp         = Time.Now();
            string outcome           = l4j.ToJson(new StringWriter(), "testException", timeStamp, "INFO"
                                                  , "quoted\"", "new line\n and {}", ti).ToString();

            Println("testException", outcome);
        }
Ejemplo n.º 4
0
        public virtual int CountExceptionsWithMessage(string text)
        {
            int count = 0;

            foreach (LoggingEvent e in GetLog())
            {
                ThrowableInformation t = e.GetThrowableInformation();
                if (t != null)
                {
                    string m = t.GetThrowable().Message;
                    if (m.Contains(text))
                    {
                        count++;
                    }
                }
            }
            return(count);
        }
Ejemplo n.º 5
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);
        }