-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NoNamespaceXmlWriter (inconclusive) experiment
- Loading branch information
Showing
7 changed files
with
115 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.IO; | ||
using System.Xml; | ||
|
||
namespace Serilog.Formatting.Log4Net; | ||
|
||
// Inspired by https://www.hanselman.com/blog/xmlfragmentwriter-omiting-the-xml-declaration-and-the-xsd-and-xsi-namespaces but does not actually work since the xmlns stack can't be manipulated that easily. | ||
internal sealed class NoNamespaceXmlWriter : XmlTextWriter | ||
{ | ||
private readonly XmlQualifiedName _ns; | ||
private bool _skipAttribute; | ||
|
||
// log4j writes the XML "manually", see https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137-L145 | ||
// The resulting XML is impossible to write with a standard compliant XML writer such as XmlWriter. | ||
// That's why we write the event in a StringWriter then massage the output to remove the xmlns:log4j attribute to match log4j output. | ||
// The XML fragment becomes valid when surrounded by an external entity, see https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L31-L49 | ||
public NoNamespaceXmlWriter(TextWriter output, XmlQualifiedName ns) : base(output) | ||
{ | ||
_ns = ns; | ||
} | ||
|
||
public override void WriteEndAttribute() | ||
{ | ||
if (_skipAttribute) | ||
{ | ||
_skipAttribute = false; | ||
} | ||
else | ||
{ | ||
base.WriteEndAttribute(); | ||
} | ||
} | ||
|
||
public override void WriteStartAttribute(string? prefix, string localName, string? ns) | ||
{ | ||
// Actually that's not how writing XML namespaces work... | ||
if (prefix == "xmlns" && localName == _ns.Name) | ||
{ | ||
_skipAttribute = true; | ||
} | ||
else | ||
{ | ||
base.WriteStartAttribute(prefix, localName, ns); | ||
} | ||
} | ||
|
||
public override void WriteString(string? text) | ||
{ | ||
if (!_skipAttribute) | ||
base.WriteString(text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<log4j:event timestamp="1041689366535" level="INFO"> | ||
<log4j:message><![CDATA[Hello from Serilog]]></log4j:message> | ||
</log4j:event> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters