replace #ifdefs in XmlLayout for netstandard with extension methods
diff --git a/src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs b/src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs
new file mode 100644
index 0000000..897ad3f
--- /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
+{
+ /// <summary>
+ /// Writes the specified start tag and associates it with the given namespace and prefix
+ /// </summary>
+ /// <param name="writer">Writer</param>
+ /// <param name="fullName">The full name of the element</param>
+ /// <param name="prefix">The namespace prefix of the element (ignored for net4x)</param>
+ /// <param name="localName">The local name of the element (ignored for net4x)</param>
+ /// <param name="ns">The namespace URI to associate with the element (ignored for net4x)</param>
+ internal static void WriteStartElement(this XmlWriter writer,
+ string fullName, string prefix, string localName, string ns)
+ => writer.WriteStartElement(fullName);
+
+ /// <summary>
+ /// Creates an XmlWriter
+ /// </summary>
+ /// <param name="writer">TextWriter</param>
+ /// <returns>XmlWriter</returns>
+ 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 0000000..341f6a0
--- /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
+ };
+
+ /// <summary>
+ /// writes the specified start tag and associates it with the given namespace and prefix
+ /// </summary>
+ /// <param name="writer">Writer</param>
+ /// <param name="fullName">The full name of the element (ignored for netstandard)</param>
+ /// <param name="prefix">The namespace prefix of the element</param>
+ /// <param name="localName">The local name of the element</param>
+ /// <param name="ns">The namespace URI to associate with the element</param>
+ internal static void WriteStartElement(this XmlWriter writer,
+ string fullName, string prefix, string localName, string ns)
+ => writer.WriteStartElement(prefix, localName, ns);
+
+ /// <summary>
+ /// Creates an XmlWriter
+ /// </summary>
+ /// <param name="writer">TextWriter</param>
+ /// <returns>XmlWriter</returns>
+ 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 0000000..d5b17d5
--- /dev/null
+++ b/src/log4net/Layout/Internal/XmlWriterExtensions.cs
@@ -0,0 +1,10 @@
+namespace log4net.Layout.Internal;
+
+/// <summary>
+/// Extensions for <see cref="System.Xml.XmlWriter"/>
+/// </summary>
+/// <author>Jan Friedrich</author>
+[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 b30686a..c23ecf9 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 @@
/// </remarks>
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 @@
}
// 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 @@
// 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 @@
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 @@
{
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 eb57228..798fcc6 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 @@
{
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 @@
#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 448b145..ef69e23 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 @@
<log4j:data name="some string" value="some valuethird"/>
</log4j:MDC>
<log4j:throwable><![CDATA[java.lang.Exception: someexception-third
- at org.apache.log4j.chainsaw.Generator.run(Generator.java:94)
+ at org.apache.log4j.chainsaw.Generator.run(Generator.java:94)
]]></log4j:throwable>
<log4j:locationInfo class="org.apache.log4j.chainsaw.Generator"
method="run" file="Generator.java" line="94"/>
@@ -172,11 +173,7 @@
}
// 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 @@
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 @@
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 @@
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 @@
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 @@
{
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 @@
writer.WriteEndElement();
}
}
-}
-
+}
\ No newline at end of file
diff --git a/src/log4net/log4net.csproj b/src/log4net/log4net.csproj
index 12a0e02..b641bcc 100644
--- a/src/log4net/log4net.csproj
+++ b/src/log4net/log4net.csproj
@@ -106,8 +106,13 @@
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
- <ItemGroup>
- <Compile Remove="Log4netAssemblyInfo.cs" />
+ <ItemGroup Condition="$(TargetFramework.StartsWith('net4'))">
+ <None Include="**\*.NetStandard.cs" />
+ <Compile Remove="**\*.NetStandard.cs" />
+ </ItemGroup>
+ <ItemGroup Condition="$(TargetFramework.StartsWith('netstandard'))">
+ <None Include="**\*.NetFx.cs" />
+ <Compile Remove="**\*.NetFx.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\log4net.snk">