Ejemplo n.º 1
0
        /// <summary>
        /// Renders the specified NDC item and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected internal override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            string msg;

            if (TopFrames != -1)
            {
                msg = NDC.GetTopMessages(TopFrames, Separator);
            }
            else if (BottomFrames != -1)
            {
                msg = NDC.GetBottomMessages(BottomFrames, Separator);
            }
            else
            {
                msg = NDC.GetAllMessages(Separator);
            }
            builder.Append(ApplyPadding(msg));
        }
Ejemplo n.º 2
0
        public void NDCTest2()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 500;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        NDC.Clear();
                        Assert.AreEqual(string.Empty, NDC.TopMessage);
                        Assert.AreEqual(string.Empty, NDC.Pop());
                        AssertContents(NDC.GetAllMessages());
                        using (NDC.Push("foo"))
                        {
                            Assert.AreEqual("foo", NDC.TopMessage);
                            AssertContents(NDC.GetAllMessages(), "foo");
                            using (NDC.Push("bar"))
                            {
                                AssertContents(NDC.GetAllMessages(), "bar", "foo");
                                Assert.AreEqual("bar", NDC.TopMessage);
                                NDC.Push("baz");
                                AssertContents(NDC.GetAllMessages(), "baz", "bar", "foo");
                                Assert.AreEqual("baz", NDC.TopMessage);
                                Assert.AreEqual("baz", NDC.Pop());

                                AssertContents(NDC.GetAllMessages(), "bar", "foo");
                                Assert.AreEqual("bar", NDC.TopMessage);
                            }

                            AssertContents(NDC.GetAllMessages(), "foo");
                            Assert.AreEqual("foo", NDC.TopMessage);
                        }

                        AssertContents(NDC.GetAllMessages());
                        Assert.AreEqual(string.Empty, NDC.Pop());
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            Assert.AreEqual(0, exceptions.Count);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Renders the XML logging event and appends it to the specified <see cref="StringBuilder" />.
        /// </summary>
        /// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param>
        /// <param name="logEvent">Logging event.</param>
        protected internal override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            StringWriter  sw  = new StringWriter(builder);
            XmlTextWriter xtw = new XmlTextWriter(sw);

            if (IndentXml)
            {
                xtw.Formatting = Formatting.Indented;
            }

            xtw.WriteStartElement("log4j:event");
            xtw.WriteAttributeString("logger", logEvent.LoggerName);
            xtw.WriteAttributeString("level", logEvent.Level.Name.ToUpper());
            xtw.WriteAttributeString("timestamp", Convert.ToString((long)(logEvent.TimeStamp.ToUniversalTime() - _log4jDateBase).TotalMilliseconds));
#if !NETCF
            xtw.WriteAttributeString("thread", NLog.Internal.ThreadIDHelper.Instance.CurrentThreadID.ToString());
#else
            xtw.WriteElementString("thread", "");
#endif

            xtw.WriteElementString("log4j:message", logEvent.FormattedMessage);
            if (IncludeNDC)
            {
                xtw.WriteElementString("log4j:NDC", NDC.GetAllMessages(" "));
            }
#if !NETCF
            if (IncludeCallSite || IncludeSourceInfo)
            {
                System.Diagnostics.StackFrame frame = logEvent.UserStackFrame;
                MethodBase methodBase = frame.GetMethod();
                Type       type       = methodBase.DeclaringType;

                xtw.WriteStartElement("log4j:locationinfo");
                xtw.WriteAttributeString("class", type.FullName);
                xtw.WriteAttributeString("method", methodBase.ToString());
                if (IncludeSourceInfo)
                {
                    xtw.WriteAttributeString("file", frame.GetFileName());
                    xtw.WriteAttributeString("line", frame.GetFileLineNumber().ToString());
                }
                xtw.WriteEndElement();

                if (IncludeNLogData)
                {
                    xtw.WriteElementString("nlog:eventSequenceNumber", logEvent.SequenceID.ToString());
                    xtw.WriteStartElement("nlog:locationinfo");
                    xtw.WriteAttributeString("assembly", type.Assembly.FullName);
                    xtw.WriteEndElement();
                }
            }
#endif
            xtw.WriteStartElement("log4j:properties");
            if (IncludeMDC)
            {
                foreach (System.Collections.DictionaryEntry entry in MDC.GetThreadDictionary())
                {
                    xtw.WriteStartElement("log4j:data");
                    xtw.WriteAttributeString("name", Convert.ToString(entry.Key));
                    xtw.WriteAttributeString("value", Convert.ToString(entry.Value));
                    xtw.WriteEndElement();
                }
            }

            foreach (NLogViewerParameterInfo parameter in Parameters)
            {
                xtw.WriteStartElement("log4j:data");
                xtw.WriteAttributeString("name", parameter.Name);
                xtw.WriteAttributeString("value", parameter.CompiledLayout.GetFormattedMessage(logEvent));
                xtw.WriteEndElement();
            }

            xtw.WriteStartElement("log4j:data");
            xtw.WriteAttributeString("name", "log4japp");
            xtw.WriteAttributeString("value", AppInfo);
            xtw.WriteEndElement();

            xtw.WriteStartElement("log4j:data");
            xtw.WriteAttributeString("name", "log4jmachinename");
#if NETCF
            xtw.WriteAttributeString("value", "netcf");
#else
            xtw.WriteAttributeString("value", NLog.LayoutRenderers.MachineNameLayoutRenderer.MachineName);
#endif
            xtw.WriteEndElement();
            xtw.WriteEndElement();

            xtw.WriteEndElement();
            xtw.Flush();
        }
Ejemplo n.º 4
0
        public void NDCTest2_object()
        {
            List <Exception> exceptions = new List <Exception>();
            ManualResetEvent mre        = new ManualResetEvent(false);
            int counter   = 100;
            int remaining = counter;

            for (int i = 0; i < counter; ++i)
            {
                ThreadPool.QueueUserWorkItem(
                    s =>
                {
                    try
                    {
                        NDC.Clear();
                        Assert.Null(NDC.TopObject);
                        Assert.Null(NDC.PopObject());
                        AssertContents(NDC.GetAllMessages());
                        using (NDC.Push("foo"))
                        {
                            Assert.Equal("foo", NDC.TopObject);
                            AssertContents(NDC.GetAllObjects(), "foo");
                            using (NDC.Push("bar"))
                            {
                                AssertContents(NDC.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NDC.TopObject);
                                NDC.Push("baz");
                                AssertContents(NDC.GetAllObjects(), "baz", "bar", "foo");
                                Assert.Equal("baz", NDC.TopObject);
                                Assert.Equal("baz", NDC.PopObject());

                                AssertContents(NDC.GetAllObjects(), "bar", "foo");
                                Assert.Equal("bar", NDC.TopObject);
                            }

                            AssertContents(NDC.GetAllObjects(), "foo");
                            Assert.Equal("foo", NDC.TopObject);
                        }

                        AssertContents(NDC.GetAllMessages());
                        Assert.Null(NDC.PopObject());
                    }
                    catch (Exception ex)
                    {
                        lock (exceptions)
                        {
                            exceptions.Add(ex);
                        }
                    }
                    finally
                    {
                        if (Interlocked.Decrement(ref remaining) == 0)
                        {
                            mre.Set();
                        }
                    }
                });
            }

            mre.WaitOne();
            StringBuilder exceptionsMessage = new StringBuilder();

            foreach (var ex in exceptions)
            {
                if (exceptionsMessage.Length > 0)
                {
                    exceptionsMessage.Append("\r\n");
                }

                exceptionsMessage.Append(ex.ToString());
            }

            Assert.True(exceptions.Count == 0, exceptionsMessage.ToString());
        }