/// <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", Common.AsyncHelpers.GetManagedThreadId().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()); } AppendNdc(xtw); AppendException(logEvent, xtw); AppendCallSite(logEvent, xtw); AppendProperties(xtw); AppendMdlc(xtw); if (IncludeAllProperties) { AppendProperties("log4j", xtw, logEvent); } AppendParameters(logEvent, xtw); 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); sb.CopyTo(builder); // StringBuilder.Replace is not good when reusing the StringBuilder } }
/// <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("logger", LoggerName != null ? LoggerName.Render(logEvent) : logEvent.LoggerName); xtw.WriteAttributeString("level", logEvent.Level.Name.ToUpperInvariant()); xtw.WriteAttributeString("timestamp", Convert.ToString((long)(logEvent.TimeStamp.ToUniversalTime() - Log4jDateBase).TotalMilliseconds, CultureInfo.InvariantCulture)); xtw.WriteAttributeString("thread", Convert.ToString(GetTaskId(logEvent))); xtw.WriteElementSafeString("log4j", "message", DummyNamespace, Message != null ? RenderLogEvent(Message, logEvent) : logEvent.FormattedMessage); xtw.WriteStartElement("log4j", "properties", DummyNamespace); if (IncludeAllProperties) { AppendProperties("log4j", DummyNamespace, xtw, logEvent); } AppendParameters(logEvent, xtw); xtw.WriteStartElement("log4j", "data", DummyNamespace); xtw.WriteAttributeString("name", "log4japp"); xtw.WriteAttributeSafeString("value", AppInfo?.Render(logEvent) ?? GetApplication(logEvent)); xtw.WriteEndElement(); xtw.WriteStartElement("log4j", "data", DummyNamespace); xtw.WriteAttributeString("name", "log4jmachinename"); xtw.WriteAttributeSafeString("value", GetSource(logEvent)); xtw.WriteEndElement(); xtw.WriteEndElement(); // properties xtw.WriteEndElement(); // event xtw.Flush(); // get rid of 'nlog' and 'log4j' namespace declarations sb.Replace(DummyNamespaceRemover, string.Empty); // copy to builder builder.Append(sb); } }
/// <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 } }