Skip to content

Commit

Permalink
replace #ifdefs in XmlLayout for netstandard with extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil committed Mar 5, 2024
1 parent c9e63b6 commit 2e5a79a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 88 deletions.
32 changes: 32 additions & 0 deletions src/log4net/Layout/Internal/XmlWriterExtensions.NetFx.cs
Original file line number Diff line number Diff line change
@@ -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
};
}
34 changes: 34 additions & 0 deletions src/log4net/Layout/Internal/XmlWriterExtensions.NetStandard.cs
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions src/log4net/Layout/Internal/XmlWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{ }
42 changes: 7 additions & 35 deletions src/log4net/Layout/XmlLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Xml;

using log4net.Core;
using log4net.Layout.Internal;
using log4net.Util;

namespace log4net.Layout
Expand Down Expand Up @@ -216,19 +217,10 @@ public override void ActivateOptions()
/// </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);
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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();
}
Expand All @@ -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);
Expand Down
16 changes: 3 additions & 13 deletions src/log4net/Layout/XmlLayoutBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using log4net.Util;
using log4net.Core;
using log4net.Layout.Internal;

namespace log4net.Layout
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -252,4 +242,4 @@ public override void Format(TextWriter writer, LoggingEvent loggingEvent)

#endregion Private Instance Fields
}
}
}
48 changes: 10 additions & 38 deletions src/log4net/Layout/XmlLayoutSchemaLog4j.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

using log4net.Core;
using log4net.Util;
using log4net.Layout.Internal;

namespace log4net.Layout
{
Expand Down Expand Up @@ -112,7 +113,7 @@ public string Version
<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"/>
Expand Down Expand Up @@ -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
Expand All @@ -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();

Expand All @@ -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();
}
Expand All @@ -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
Expand All @@ -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();
}
Expand All @@ -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);
Expand All @@ -276,5 +249,4 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
writer.WriteEndElement();
}
}
}

}
9 changes: 7 additions & 2 deletions src/log4net/log4net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down

0 comments on commit 2e5a79a

Please sign in to comment.