From 2e5a79adaea383aaeb202886df0012192d5857be Mon Sep 17 00:00:00 2001 From: Jan Friedrich Date: Tue, 5 Mar 2024 13:24:11 +0100 Subject: [PATCH] replace #ifdefs in XmlLayout for netstandard with extension methods --- .../Internal/XmlWriterExtensions.NetFx.cs | 32 +++++++++++++ .../XmlWriterExtensions.NetStandard.cs | 34 +++++++++++++ .../Layout/Internal/XmlWriterExtensions.cs | 10 ++++ src/log4net/Layout/XmlLayout.cs | 42 +++------------- src/log4net/Layout/XmlLayoutBase.cs | 16 ++----- src/log4net/Layout/XmlLayoutSchemaLog4j.cs | 48 ++++--------------- src/log4net/log4net.csproj | 9 +++- 7 files changed, 103 insertions(+), 88 deletions(-) create mode 100644 src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs create mode 100644 src/log4net/Layout/Internal/XmlWriterExtensions.NetStandard.cs create mode 100644 src/log4net/Layout/Internal/XmlWriterExtensions.cs diff --git a/src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs b/src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs new file mode 100644 index 00000000..897ad3fd --- /dev/null +++ b/src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs @@ -0,0 +1,32 @@ +using System.IO; +using System.Xml; +using log4net.Util; + +namespace log4net.Layout.Internal; + +partial class XmlWriterExtensions +{ + /// + /// Writes the specified start tag and associates it with the given namespace and prefix + /// + /// Writer + /// The full name of the element + /// The namespace prefix of the element (ignored for net4x) + /// The local name of the element (ignored for net4x) + /// The namespace URI to associate with the element (ignored for net4x) + internal static void WriteStartElement(this XmlWriter writer, + string fullName, string prefix, string localName, string ns) + => writer.WriteStartElement(fullName); + + /// + /// Creates an XmlWriter + /// + /// TextWriter + /// XmlWriter + internal static XmlWriter CreateXmlWriter(TextWriter writer) + => new XmlTextWriter(new ProtectCloseTextWriter(writer)) + { + Formatting = Formatting.None, + Namespaces = false + }; +} diff --git a/src/log4net/Layout/Internal/XmlWriterExtensions.NetStandard.cs b/src/log4net/Layout/Internal/XmlWriterExtensions.NetStandard.cs new file mode 100644 index 00000000..341f6a00 --- /dev/null +++ b/src/log4net/Layout/Internal/XmlWriterExtensions.NetStandard.cs @@ -0,0 +1,34 @@ +using System.IO; +using System.Xml; +using log4net.Util; + +namespace log4net.Layout.Internal; + +partial class XmlWriterExtensions +{ + private static readonly XmlWriterSettings settings = new XmlWriterSettings + { + Indent = false, + OmitXmlDeclaration = true + }; + + /// + /// writes the specified start tag and associates it with the given namespace and prefix + /// + /// Writer + /// The full name of the element (ignored for netstandard) + /// The namespace prefix of the element + /// The local name of the element + /// The namespace URI to associate with the element + internal static void WriteStartElement(this XmlWriter writer, + string fullName, string prefix, string localName, string ns) + => writer.WriteStartElement(prefix, localName, ns); + + /// + /// Creates an XmlWriter + /// + /// TextWriter + /// XmlWriter + internal static XmlWriter CreateXmlWriter(TextWriter writer) + => XmlWriter.Create(new ProtectCloseTextWriter(writer), settings); +} \ No newline at end of file diff --git a/src/log4net/Layout/Internal/XmlWriterExtensions.cs b/src/log4net/Layout/Internal/XmlWriterExtensions.cs new file mode 100644 index 00000000..d5b17d58 --- /dev/null +++ b/src/log4net/Layout/Internal/XmlWriterExtensions.cs @@ -0,0 +1,10 @@ +namespace log4net.Layout.Internal; + +/// +/// Extensions for +/// +/// Jan Friedrich +[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter", + Justification = "Compatibility between net4 and netstandard")] +internal static partial class XmlWriterExtensions +{ } \ No newline at end of file diff --git a/src/log4net/Layout/XmlLayout.cs b/src/log4net/Layout/XmlLayout.cs index b30686a6..c23ecf9a 100644 --- a/src/log4net/Layout/XmlLayout.cs +++ b/src/log4net/Layout/XmlLayout.cs @@ -22,6 +22,7 @@ using System.Xml; using log4net.Core; +using log4net.Layout.Internal; using log4net.Util; namespace log4net.Layout @@ -216,19 +217,10 @@ public override void ActivateOptions() /// protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) { -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_EVENT, m_prefix); - // writer.WriteAttributeString("xmlns", "log4net", null, "http://logging.apache.org/log4net/schemas/log4net-events-1.2"); -#else - writer.WriteStartElement(m_elmEvent); -#endif + writer.WriteStartElement(m_elmEvent, m_prefix, ELM_EVENT, m_prefix); writer.WriteAttributeString(ATTR_LOGGER, loggingEvent.LoggerName); -#if NET_2_0 || NETCF_2_0 || MONO_2_0 || NETSTANDARD writer.WriteAttributeString(ATTR_TIMESTAMP, XmlConvert.ToString(loggingEvent.TimeStamp, XmlDateTimeSerializationMode.Local)); -#else - writer.WriteAttributeString(ATTR_TIMESTAMP, XmlConvert.ToString(loggingEvent.TimeStamp)); -#endif writer.WriteAttributeString(ATTR_LEVEL, loggingEvent.Level.DisplayName); writer.WriteAttributeString(ATTR_THREAD, loggingEvent.ThreadName); @@ -247,11 +239,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) } // Append the message text -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_MESSAGE, m_prefix); -#else - writer.WriteStartElement(m_elmMessage); -#endif + writer.WriteStartElement(m_elmMessage, m_prefix, ELM_MESSAGE, m_prefix); if (!this.Base64EncodeMessage) { Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage, this.InvalidCharReplacement); @@ -269,18 +257,10 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) // Append the properties text if (properties.Count > 0) { -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_PROPERTIES, m_prefix); -#else - writer.WriteStartElement(m_elmProperties); -#endif + writer.WriteStartElement(m_elmProperties, m_prefix, ELM_PROPERTIES, m_prefix); foreach (System.Collections.DictionaryEntry entry in properties) { -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_DATA, m_prefix); -#else - writer.WriteStartElement(m_elmData); -#endif + writer.WriteStartElement(m_elmData, m_prefix, ELM_DATA, m_prefix); writer.WriteAttributeString(ATTR_NAME, Transform.MaskXmlInvalidCharacters((string)entry.Key, this.InvalidCharReplacement)); // Use an ObjectRenderer to convert the object to a string @@ -305,11 +285,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) if (exceptionStr != null && exceptionStr.Length > 0) { // Append the stack trace line -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_EXCEPTION, m_prefix); -#else - writer.WriteStartElement(m_elmException); -#endif + writer.WriteStartElement(m_elmException, m_prefix, ELM_EXCEPTION, m_prefix); Transform.WriteEscapedXmlString(writer, exceptionStr, this.InvalidCharReplacement); writer.WriteEndElement(); } @@ -318,11 +294,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) { LocationInfo locationInfo = loggingEvent.LocationInformation; -#if NETSTANDARD - writer.WriteStartElement(m_prefix, ELM_LOCATION, m_prefix); -#else - writer.WriteStartElement(m_elmLocation); -#endif + writer.WriteStartElement(m_elmLocation, m_prefix, ELM_LOCATION, m_prefix); writer.WriteAttributeString(ATTR_CLASS, locationInfo.ClassName); writer.WriteAttributeString(ATTR_METHOD, locationInfo.MethodName); writer.WriteAttributeString(ATTR_FILE, locationInfo.FileName); diff --git a/src/log4net/Layout/XmlLayoutBase.cs b/src/log4net/Layout/XmlLayoutBase.cs index eb572284..798fcc6b 100644 --- a/src/log4net/Layout/XmlLayoutBase.cs +++ b/src/log4net/Layout/XmlLayoutBase.cs @@ -24,6 +24,7 @@ using log4net.Util; using log4net.Core; +using log4net.Layout.Internal; namespace log4net.Layout { @@ -196,18 +197,7 @@ public override void Format(TextWriter writer, LoggingEvent loggingEvent) { throw new ArgumentNullException("loggingEvent"); } -#if NETSTANDARD - var settings = new XmlWriterSettings - { - Indent = false, - OmitXmlDeclaration = true - }; - using var xmlWriter = XmlWriter.Create(new ProtectCloseTextWriter(writer), settings); -#else - using XmlTextWriter xmlWriter = new XmlTextWriter(new ProtectCloseTextWriter(writer)); - xmlWriter.Formatting = Formatting.None; - xmlWriter.Namespaces = false; -#endif + using XmlWriter xmlWriter = XmlWriterExtensions.CreateXmlWriter(writer); // Write the event to the writer FormatXml(xmlWriter, loggingEvent); @@ -252,4 +242,4 @@ public override void Format(TextWriter writer, LoggingEvent loggingEvent) #endregion Private Instance Fields } -} +} \ No newline at end of file diff --git a/src/log4net/Layout/XmlLayoutSchemaLog4j.cs b/src/log4net/Layout/XmlLayoutSchemaLog4j.cs index 448b145f..ef69e23f 100644 --- a/src/log4net/Layout/XmlLayoutSchemaLog4j.cs +++ b/src/log4net/Layout/XmlLayoutSchemaLog4j.cs @@ -24,6 +24,7 @@ using log4net.Core; using log4net.Util; +using log4net.Layout.Internal; namespace log4net.Layout { @@ -112,7 +113,7 @@ public string Version @@ -172,11 +173,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) } // Write the start element -#if NETSTANDARD - writer.WriteStartElement("log4j", "event", "log4net"); -#else - writer.WriteStartElement("log4j:event"); -#endif + writer.WriteStartElement("log4j:event", "log4j", "event", "log4net"); writer.WriteAttributeString("logger", loggingEvent.LoggerName); // Calculate the timestamp as the number of milliseconds since january 1970 @@ -191,11 +188,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) writer.WriteAttributeString("thread", loggingEvent.ThreadName); // Append the message text -#if NETSTANDARD - writer.WriteStartElement("log4j", "message", "log4net"); -#else - writer.WriteStartElement("log4j:message"); -#endif + writer.WriteStartElement("log4j:message", "log4j", "message", "log4net"); Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage, this.InvalidCharReplacement); writer.WriteEndElement(); @@ -207,11 +200,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) if (valueStr != null && valueStr.Length > 0) { // Append the NDC text -#if NETSTANDARD - writer.WriteStartElement("log4j", "NDC", "log4net"); -#else - writer.WriteStartElement("log4j:NDC"); -#endif + writer.WriteStartElement("log4j:NDC", "log4j", "NDC", "log4net"); Transform.WriteEscapedXmlString(writer, valueStr, this.InvalidCharReplacement); writer.WriteEndElement(); } @@ -221,18 +210,10 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) PropertiesDictionary properties = loggingEvent.GetProperties(); if (properties.Count > 0) { -#if NETSTANDARD - writer.WriteStartElement("log4j", "properties", "log4net"); -#else - writer.WriteStartElement("log4j:properties"); -#endif + writer.WriteStartElement("log4j:properties", "log4j", "properties", "log4net"); foreach (System.Collections.DictionaryEntry entry in properties) { -#if NETSTANDARD - writer.WriteStartElement("log4j", "data", "log4net"); -#else - writer.WriteStartElement("log4j:data"); -#endif + writer.WriteStartElement("log4j:data", "log4j", "data", "log4net"); writer.WriteAttributeString("name", (string)entry.Key); // Use an ObjectRenderer to convert the object to a string @@ -248,11 +229,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) if (exceptionStr != null && exceptionStr.Length > 0) { // Append the stack trace line -#if NETSTANDARD - writer.WriteStartElement("log4j", "throwable", "log4net"); -#else - writer.WriteStartElement("log4j:throwable"); -#endif + writer.WriteStartElement("log4j:throwable", "log4j", "data", "log4net"); Transform.WriteEscapedXmlString(writer, exceptionStr, this.InvalidCharReplacement); writer.WriteEndElement(); } @@ -261,11 +238,7 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) { LocationInfo locationInfo = loggingEvent.LocationInformation; -#if NETSTANDARD - writer.WriteStartElement("log4j", "locationInfo", "log4net"); -#else - writer.WriteStartElement("log4j:locationInfo"); -#endif + writer.WriteStartElement("log4j:locationInfo", "log4j", "locationInfo", "log4net"); writer.WriteAttributeString("class", locationInfo.ClassName); writer.WriteAttributeString("method", locationInfo.MethodName); writer.WriteAttributeString("file", locationInfo.FileName); @@ -276,5 +249,4 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent) writer.WriteEndElement(); } } -} - +} \ No newline at end of file diff --git a/src/log4net/log4net.csproj b/src/log4net/log4net.csproj index 12a0e025..b641bccd 100644 --- a/src/log4net/log4net.csproj +++ b/src/log4net/log4net.csproj @@ -106,8 +106,13 @@ - - + + + + + + +