This is an automated email from the ASF dual-hosted git repository.

freeandnil pushed a commit to branch Feature/InvalidXmlCharacters
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git

commit 933c3407f7abc04bbf02347cd548d37d2dbf706b
Author: Jan Friedrich <[email protected]>
AuthorDate: Mon Feb 16 23:22:25 2026 +0100

    Zwischenstand
---
 .../Layout/XmlLayoutSchemaLog4jTest.cs             | 45 +++++++++++++++++++++-
 src/log4net/Layout/Internal/XmlWriterExtensions.cs | 17 +-------
 src/log4net/Layout/XmlLayoutSchemaLog4j.cs         |  5 ++-
 3 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/src/log4net.Tests/Layout/XmlLayoutSchemaLog4jTest.cs 
b/src/log4net.Tests/Layout/XmlLayoutSchemaLog4jTest.cs
index d7d14de0..6aabd012 100644
--- a/src/log4net.Tests/Layout/XmlLayoutSchemaLog4jTest.cs
+++ b/src/log4net.Tests/Layout/XmlLayoutSchemaLog4jTest.cs
@@ -18,12 +18,11 @@
 #endregion
 
 using System;
-
 using log4net.Config;
+using log4net.Core;
 using log4net.Layout;
 using log4net.Repository;
 using log4net.Tests.Appender;
-
 using NUnit.Framework;
 
 namespace log4net.Tests.Layout
@@ -63,5 +62,47 @@ void ThrowAndLog(int foo)
         }
       }
     }
+    
+    /// <summary>
+    /// Tests the serialization of invalid characters in the Properties 
dictionary
+    /// </summary>
+    [Test]
+    public void InvalidCharacterTest()
+    {
+      StringAppender stringAppender = new() { Layout = new 
XmlLayoutSchemaLog4J() };
+
+      ILoggerRepository repository = 
LogManager.CreateRepository(Guid.NewGuid().ToString());
+      BasicConfigurator.Configure(repository, stringAppender);
+      ILog log = LogManager.GetLogger(repository.Name, "TestLogger");
+      
+      Log();
+
+      string logEventXml = stringAppender.GetString();
+      Assert.That(logEventXml, Does.Contain("us?er"));
+      Assert.That(logEventXml, Does.Contain("A?B"));
+      void Log()
+      {
+        // Build a LoggingEvent with an XML invalid character in a property 
value
+        LoggingEventData data = new()
+        {
+          LoggerName = "Logger",
+          Level = Level.Info,
+          TimeStampUtc = DateTime.UtcNow,
+          ThreadName = "ThreadName",
+          Domain = "Domain",
+          Identity = "Identity",
+          UserName = "UserName",
+          Message = "Message",
+          ExceptionString = "",
+          Properties = new()
+        };
+
+        // Value contains U+0001 which is illegal in XML 1.0
+        data.Properties["us\u0001er"] = "A\u0001B";
+
+        // Log the event
+        log.Logger.Log(new(null, repository, data));
+      }
+    }
   }
 }
\ No newline at end of file
diff --git a/src/log4net/Layout/Internal/XmlWriterExtensions.cs 
b/src/log4net/Layout/Internal/XmlWriterExtensions.cs
index e08b759d..20c1d5b2 100644
--- a/src/log4net/Layout/Internal/XmlWriterExtensions.cs
+++ b/src/log4net/Layout/Internal/XmlWriterExtensions.cs
@@ -31,13 +31,12 @@ namespace log4net.Layout.Internal;
   Justification = "Compatibility between net4 and netstandard")]
 internal static partial class XmlWriterExtensions
 {
-#if NETSTANDARD2_0_OR_GREATER
   private static readonly XmlWriterSettings _settings = new XmlWriterSettings
   {
     Indent = false,
-    OmitXmlDeclaration = true
+    OmitXmlDeclaration = true,
+    //CheckCharacters = false
   };
-#endif
 
   /// <summary>
   /// writes the specified start tag and associates it with the given 
namespace and prefix
@@ -49,11 +48,7 @@ internal static partial class XmlWriterExtensions
   /// <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)
-#if NETSTANDARD2_0_OR_GREATER
     => writer.WriteStartElement(prefix, localName, ns);
-#else
-    => writer.WriteStartElement(fullName);
-#endif
 
 
   /// <summary>
@@ -62,13 +57,5 @@ internal static partial class XmlWriterExtensions
   /// <param name="writer">TextWriter</param>
   /// <returns>XmlWriter</returns>
   internal static XmlWriter CreateXmlWriter(TextWriter writer)
-#if NETSTANDARD2_0_OR_GREATER
     => XmlWriter.Create(new ProtectCloseTextWriter(writer), _settings);
-#else
-    => new XmlTextWriter(new ProtectCloseTextWriter(writer))
-    {
-      Formatting = Formatting.None,
-      Namespaces = false
-    };
-#endif
 }
\ No newline at end of file
diff --git a/src/log4net/Layout/XmlLayoutSchemaLog4j.cs 
b/src/log4net/Layout/XmlLayoutSchemaLog4j.cs
index ff2df82f..04a7f08f 100644
--- a/src/log4net/Layout/XmlLayoutSchemaLog4j.cs
+++ b/src/log4net/Layout/XmlLayoutSchemaLog4j.cs
@@ -203,13 +203,16 @@ protected override void FormatXml(XmlWriter writer, 
LoggingEvent loggingEvent)
       foreach (KeyValuePair<string, object?> entry in properties)
       {
         writer.WriteStartElement("log4j:data", "log4j", "data", "log4net");
-        writer.WriteAttributeString("name", entry.Key);
+        writer.WriteAttributeString("name", 
+          Transform.MaskXmlInvalidCharacters(entry.Key, 
InvalidCharReplacement));
 
         // Use an ObjectRenderer to convert the object to a string
         string? valueStr = 
loggingEvent.Repository?.RendererMap.FindAndRender(entry.Value);
         if (!string.IsNullOrEmpty(valueStr))
         {
           writer.WriteAttributeString("value", valueStr);
+          //writer.WriteAttributeString("value", 
+            //Transform.MaskXmlInvalidCharacters(valueStr!, 
InvalidCharReplacement));
         }
 
         writer.WriteEndElement();

Reply via email to