-
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.
- Loading branch information
Showing
4 changed files
with
97 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
using System.Xml; | ||
|
||
namespace Serilog.Formatting.Log4Net; | ||
|
||
internal sealed class Log4JXmlNamespaceResolver : IXmlNamespaceResolver | ||
{ | ||
private const string Prefix = "log4j"; | ||
private const string NamespaceName = "http://jakarta.apache.org/log4j/"; | ||
|
||
public static readonly Log4JXmlNamespaceResolver Instance = new(); | ||
|
||
/// <summary> | ||
/// The XML namespace used for Log4j events. | ||
/// </summary> | ||
/// <remarks>https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137</remarks> | ||
public static readonly XmlQualifiedName Log4JXmlNamespace = new(Prefix, NamespaceName); | ||
|
||
private static readonly Dictionary<string, string> Namespaces = new() { [Prefix] = NamespaceName }; | ||
|
||
IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope) => Namespaces; | ||
|
||
string? IXmlNamespaceResolver.LookupNamespace(string prefix) => prefix == Prefix ? NamespaceName : null; | ||
|
||
string? IXmlNamespaceResolver.LookupPrefix(string namespaceName) => namespaceName == NamespaceName ? Prefix : null; | ||
} |
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,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Xml; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Serilog.Formatting.Log4Net.Tests; | ||
|
||
public class Log4JXmlNamespaceResolverTest | ||
{ | ||
private readonly IXmlNamespaceResolver _resolver; | ||
|
||
public Log4JXmlNamespaceResolverTest() | ||
{ | ||
var log4JXmlNamespaceResolver = typeof(Log4NetTextFormatter).Assembly.GetType("Serilog.Formatting.Log4Net.Log4JXmlNamespaceResolver") | ||
?? throw new MissingMemberException("Serilog.Formatting.Log4Net.Log4JXmlNamespaceResolver"); | ||
var instance = log4JXmlNamespaceResolver.GetField("Instance", BindingFlags.Public | BindingFlags.Static) | ||
?? throw new MissingFieldException("Serilog.Formatting.Log4Net.Log4JXmlNamespaceResolver", "Instance"); | ||
_resolver = (IXmlNamespaceResolver)instance.GetValue(log4JXmlNamespaceResolver)!; | ||
} | ||
|
||
[Theory] | ||
[InlineData(XmlNamespaceScope.All)] | ||
[InlineData(XmlNamespaceScope.ExcludeXml)] | ||
[InlineData(XmlNamespaceScope.Local)] | ||
public void GetNamespacesInScope(XmlNamespaceScope scope) | ||
{ | ||
var expected = new Dictionary<string, string> { ["log4j"] = "http://jakarta.apache.org/log4j/" }; | ||
_resolver.GetNamespacesInScope(scope).Should().BeEquivalentTo(expected); | ||
} | ||
|
||
[Fact] | ||
public void LookupLog4JNamespace() | ||
{ | ||
_resolver.LookupNamespace("log4j").Should().Be("http://jakarta.apache.org/log4j/"); | ||
} | ||
|
||
[Fact] | ||
public void LookupLog4JPrefix() | ||
{ | ||
_resolver.LookupPrefix("http://jakarta.apache.org/log4j/").Should().Be("log4j"); | ||
} | ||
|
||
[Theory] | ||
[InlineData("xmlns")] | ||
[InlineData("xml")] | ||
public void LookupStandardXmlNamespace(string prefix) | ||
{ | ||
_resolver.LookupNamespace(prefix).Should().BeNull(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("http://www.w3.org/2000/xmlns/")] | ||
[InlineData("http://www.w3.org/XML/1998/namespace")] | ||
public void LookupStandardXmlPrefix(string namespaceName) | ||
{ | ||
_resolver.LookupPrefix(namespaceName).Should().BeNull(); | ||
} | ||
} |