private void AppendNdc(XmlWriter xtw)
        {
            string ndcContent = null;

            if (IncludeNdc)
            {
                ndcContent = string.Join(NdcItemSeparator, NestedDiagnosticsContext.GetAllMessages());
            }
#if !SILVERLIGHT
            if (IncludeNdlc)
            {
                if (ndcContent != null)
                {
                    //extra separator
                    ndcContent += NdcItemSeparator;
                }
                ndcContent += string.Join(NdlcItemSeparator, NestedDiagnosticsLogicalContext.GetAllMessages());
            }
#endif

            if (ndcContent != null)
            {
                //NDLC and NDC should be in the same element
                xtw.WriteElementSafeString("log4j", "NDC", dummyNamespace, ndcContent);
            }
        }
Exemple #2
0
        public void NdlcGetAllMessages()
        {
            object value = 5;

            NestedDiagnosticsLogicalContext.Clear();
            var popper = NestedDiagnosticsLogicalContext.Push(value);

            string expected = "5";

            string[] actual = NestedDiagnosticsLogicalContext.GetAllMessages();
            Assert.Single(actual);
            Assert.Equal(expected, actual[0]);

            popper.Dispose();
            actual = NestedDiagnosticsLogicalContext.GetAllMessages();
            Assert.Empty(actual);
        }
        /// <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 override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            StringBuilder sb = new StringBuilder();

            using (XmlWriter xtw = XmlWriter.Create(sb, _xmlWriterSettings))
            {
                xtw.WriteStartElement("log4j", "event", dummyNamespace);
                xtw.WriteAttributeSafeString("xmlns", "nlog", null, dummyNLogNamespace);
                xtw.WriteAttributeSafeString("logger", LoggerName != null ? LoggerName.Render(logEvent) : logEvent.LoggerName);
                xtw.WriteAttributeSafeString("level", logEvent.Level.Name.ToUpperInvariant());
                xtw.WriteAttributeSafeString("timestamp", Convert.ToString((long)(logEvent.TimeStamp.ToUniversalTime() - log4jDateBase).TotalMilliseconds, CultureInfo.InvariantCulture));
                xtw.WriteAttributeSafeString("thread", System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture));

                xtw.WriteElementSafeString("log4j", "message", dummyNamespace, logEvent.FormattedMessage);
                if (logEvent.Exception != null)
                {
                    // TODO Why twice the exception details?
                    xtw.WriteElementSafeString("log4j", "throwable", dummyNamespace, logEvent.Exception.ToString());
                }

                string ndcContent = null;
                if (IncludeNdc)
                {
                    ndcContent = string.Join(NdcItemSeparator, NestedDiagnosticsContext.GetAllMessages());
                }
#if !SILVERLIGHT
                if (IncludeNdlc)
                {
                    if (ndcContent != null)
                    {
                        //extra separator
                        ndcContent += NdcItemSeparator;
                    }
                    ndcContent += string.Join(NdlcItemSeparator, NestedDiagnosticsLogicalContext.GetAllMessages());
                }
#endif

                if (ndcContent != null)
                {
                    //NDLC and NDC should be in the same element
                    xtw.WriteElementSafeString("log4j", "NDC", dummyNamespace, ndcContent);
                }

                if (logEvent.Exception != null)
                {
                    // TODO Why twice the exception details?
                    xtw.WriteStartElement("log4j", "throwable", dummyNamespace);
                    xtw.WriteSafeCData(logEvent.Exception.ToString());
                    xtw.WriteEndElement();
                }

                if (IncludeCallSite || IncludeSourceInfo)
                {
                    if (logEvent.CallSiteInformation != null)
                    {
                        MethodBase methodBase       = logEvent.CallSiteInformation.GetCallerStackFrameMethod(0);
                        string     callerClassName  = logEvent.CallSiteInformation.GetCallerClassName(methodBase, true, true, true);
                        string     callerMemberName = logEvent.CallSiteInformation.GetCallerMemberName(methodBase, true, true, true);

                        xtw.WriteStartElement("log4j", "locationInfo", dummyNamespace);
                        if (!string.IsNullOrEmpty(callerClassName))
                        {
                            xtw.WriteAttributeSafeString("class", callerClassName);
                        }

                        xtw.WriteAttributeSafeString("method", callerMemberName);
#if !SILVERLIGHT
                        if (IncludeSourceInfo)
                        {
                            xtw.WriteAttributeSafeString("file", logEvent.CallSiteInformation.GetCallerFilePath(0));
                            xtw.WriteAttributeSafeString("line", logEvent.CallSiteInformation.GetCallerLineNumber(0).ToString(CultureInfo.InvariantCulture));
                        }
#endif
                        xtw.WriteEndElement();

                        if (IncludeNLogData)
                        {
                            xtw.WriteElementSafeString("nlog", "eventSequenceNumber", dummyNLogNamespace, logEvent.SequenceID.ToString(CultureInfo.InvariantCulture));
                            xtw.WriteStartElement("nlog", "locationInfo", dummyNLogNamespace);
                            var type = methodBase?.DeclaringType;
                            if (type != null)
                            {
                                xtw.WriteAttributeSafeString("assembly", type.GetAssembly().FullName);
                            }
                            xtw.WriteEndElement();

                            xtw.WriteStartElement("nlog", "properties", dummyNLogNamespace);
                            AppendProperties("nlog", xtw, logEvent);
                            xtw.WriteEndElement();
                        }
                    }
                }

                xtw.WriteStartElement("log4j", "properties", dummyNamespace);
                if (IncludeMdc)
                {
                    foreach (string key in MappedDiagnosticsContext.GetNames())
                    {
                        string propertyValue = XmlHelper.XmlConvertToString(MappedDiagnosticsContext.GetObject(key));
                        if (propertyValue == null)
                        {
                            continue;
                        }

                        xtw.WriteStartElement("log4j", "data", dummyNamespace);
                        xtw.WriteAttributeSafeString("name", key);
                        xtw.WriteAttributeSafeString("value", propertyValue);
                        xtw.WriteEndElement();
                    }
                }

#if !SILVERLIGHT
                if (IncludeMdlc)
                {
                    foreach (string key in MappedDiagnosticsLogicalContext.GetNames())
                    {
                        string propertyValue = XmlHelper.XmlConvertToString(MappedDiagnosticsLogicalContext.GetObject(key));
                        if (propertyValue == null)
                        {
                            continue;
                        }

                        xtw.WriteStartElement("log4j", "data", dummyNamespace);
                        xtw.WriteAttributeSafeString("name", key);
                        xtw.WriteAttributeSafeString("value", propertyValue);
                        xtw.WriteEndElement();
                    }
                }
#endif

                if (IncludeAllProperties)
                {
                    AppendProperties("log4j", xtw, logEvent);
                }

                if (Parameters.Count > 0)
                {
                    foreach (NLogViewerParameterInfo parameter in Parameters)
                    {
                        xtw.WriteStartElement("log4j", "data", dummyNamespace);
                        xtw.WriteAttributeSafeString("name", parameter.Name);
                        xtw.WriteAttributeSafeString("value", parameter.Layout.Render(logEvent));
                        xtw.WriteEndElement();
                    }
                }

                xtw.WriteStartElement("log4j", "data", dummyNamespace);
                xtw.WriteAttributeSafeString("name", "log4japp");
                xtw.WriteAttributeSafeString("value", AppInfo);
                xtw.WriteEndElement();

                xtw.WriteStartElement("log4j", "data", dummyNamespace);
                xtw.WriteAttributeSafeString("name", "log4jmachinename");
                xtw.WriteAttributeSafeString("value", _machineName);
                xtw.WriteEndElement();

                xtw.WriteEndElement();

                xtw.WriteEndElement();
                xtw.Flush();

                // get rid of 'nlog' and 'log4j' namespace declarations
                sb.Replace(dummyNamespaceRemover, string.Empty);
                sb.Replace(dummyNLogNamespaceRemover, string.Empty);
                builder.Append(sb.ToString());  // StringBuilder.Replace is not good when reusing the StringBuilder
            }
        }