Skip to content

Commit 17cb78d

Browse files
committed
Use file-scoped namespace in Log4NetTextFormatterOptionsBuilder too
1 parent 131368d commit 17cb78d

File tree

1 file changed

+179
-180
lines changed

1 file changed

+179
-180
lines changed

src/Log4NetTextFormatterOptionsBuilder.cs

Lines changed: 179 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -2,210 +2,209 @@
22
using System.Xml;
33
using Serilog.Events;
44

5-
namespace Serilog.Formatting.Log4Net
5+
namespace Serilog.Formatting.Log4Net;
6+
7+
/// <summary>
8+
/// A fluent builder for the options controlling how <see cref="Log4NetTextFormatter"/> writes log4net events.
9+
/// </summary>
10+
public class Log4NetTextFormatterOptionsBuilder
611
{
712
/// <summary>
8-
/// A fluent builder for the options controlling how <see cref="Log4NetTextFormatter"/> writes log4net events.
13+
/// The XML namespace used for log4net events.
914
/// </summary>
10-
public class Log4NetTextFormatterOptionsBuilder
15+
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L49</remarks>
16+
private static readonly XmlQualifiedName Log4NetXmlNamespace = new("log4net", "http://logging.apache.org/log4net/schemas/log4net-events-1.2/");
17+
18+
/// <summary>
19+
/// The XML namespace used for Log4j events.
20+
/// </summary>
21+
/// <remarks>https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137</remarks>
22+
internal static readonly XmlQualifiedName Log4JXmlNamespace = new("log4j", "http://jakarta.apache.org/log4j/");
23+
24+
/// <summary>
25+
/// Initialize a new instance of the <see cref="Log4NetTextFormatterOptionsBuilder"/> class.
26+
/// </summary>
27+
internal Log4NetTextFormatterOptionsBuilder()
1128
{
12-
/// <summary>
13-
/// The XML namespace used for log4net events.
14-
/// </summary>
15-
/// <remarks>https://github.com/apache/logging-log4net/blob/rel/2.0.8/src/Layout/XmlLayout.cs#L49</remarks>
16-
private static readonly XmlQualifiedName Log4NetXmlNamespace = new("log4net", "http://logging.apache.org/log4net/schemas/log4net-events-1.2/");
17-
18-
/// <summary>
19-
/// The XML namespace used for Log4j events.
20-
/// </summary>
21-
/// <remarks>https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137</remarks>
22-
internal static readonly XmlQualifiedName Log4JXmlNamespace = new("log4j", "http://jakarta.apache.org/log4j/");
23-
24-
/// <summary>
25-
/// Initialize a new instance of the <see cref="Log4NetTextFormatterOptionsBuilder"/> class.
26-
/// </summary>
27-
internal Log4NetTextFormatterOptionsBuilder()
28-
{
29-
}
29+
}
3030

31-
/// <summary>See <see cref="UseFormatProvider"/></summary>
32-
private IFormatProvider? _formatProvider;
31+
/// <summary>See <see cref="UseFormatProvider"/></summary>
32+
private IFormatProvider? _formatProvider;
3333

34-
/// <summary> See <see cref="UseCDataMode"/></summary>
35-
private CDataMode _cDataMode = CDataMode.Always;
34+
/// <summary> See <see cref="UseCDataMode"/></summary>
35+
private CDataMode _cDataMode = CDataMode.Always;
3636

37-
/// <summary>See <see cref="UseNoXmlNamespace"/></summary>
38-
private XmlQualifiedName? _xmlNamespace = Log4NetXmlNamespace;
37+
/// <summary>See <see cref="UseNoXmlNamespace"/></summary>
38+
private XmlQualifiedName? _xmlNamespace = Log4NetXmlNamespace;
3939

40-
/// <summary>See <see cref="UseLineEnding"/></summary>
41-
private LineEnding _lineEnding = LineEnding.LineFeed;
40+
/// <summary>See <see cref="UseLineEnding"/></summary>
41+
private LineEnding _lineEnding = LineEnding.LineFeed;
4242

43-
/// <summary>See <see cref="UseIndentationSettings"/></summary>
44-
private IndentationSettings? _indentationSettings = new(Indentation.Space, size: 2);
43+
/// <summary>See <see cref="UseIndentationSettings"/></summary>
44+
private IndentationSettings? _indentationSettings = new(Indentation.Space, size: 2);
4545

46-
/// <summary>See <see cref="UsePropertyFilter"/></summary>
47-
private PropertyFilter _filterProperty = (_, _) => true;
46+
/// <summary>See <see cref="UsePropertyFilter"/></summary>
47+
private PropertyFilter _filterProperty = (_, _) => true;
4848

49-
/// <summary>See <see cref="UseExceptionFormatter"/></summary>
50-
private ExceptionFormatter _formatException = exception => exception.ToString();
49+
/// <summary>See <see cref="UseExceptionFormatter"/></summary>
50+
private ExceptionFormatter _formatException = exception => exception.ToString();
5151

52-
/// <summary>
53-
/// Sets the <see cref="IFormatProvider"/> used when formatting message and properties of log4net events.
54-
/// <para/>
55-
/// The default value is <see langword="null"/>, meaning that the default Serilog format provider is used, i.e. the invariant culture.
56-
/// </summary>
57-
/// <param name="formatProvider">The <see cref="IFormatProvider"/> to use.</param>
58-
/// <returns>The builder in order to fluently chain all options.</returns>
59-
public Log4NetTextFormatterOptionsBuilder UseFormatProvider(IFormatProvider? formatProvider)
60-
{
61-
_formatProvider = formatProvider;
62-
return this;
63-
}
52+
/// <summary>
53+
/// Sets the <see cref="IFormatProvider"/> used when formatting message and properties of log4net events.
54+
/// <para/>
55+
/// The default value is <see langword="null"/>, meaning that the default Serilog format provider is used, i.e. the invariant culture.
56+
/// </summary>
57+
/// <param name="formatProvider">The <see cref="IFormatProvider"/> to use.</param>
58+
/// <returns>The builder in order to fluently chain all options.</returns>
59+
public Log4NetTextFormatterOptionsBuilder UseFormatProvider(IFormatProvider? formatProvider)
60+
{
61+
_formatProvider = formatProvider;
62+
return this;
63+
}
6464

65-
/// <summary>
66-
/// Sets the <see cref="CDataMode"/> controlling how <c>message</c> and <c>exception</c> XML elements of log4net events are written.
67-
/// <para/>
68-
/// The default value is <see cref="Log4Net.CDataMode.Always"/>.
69-
/// </summary>
70-
/// <param name="cDataMode">The <see cref="CDataMode"/> to use.</param>
71-
/// <returns>The builder in order to fluently chain all options.</returns>
72-
public Log4NetTextFormatterOptionsBuilder UseCDataMode(CDataMode cDataMode)
73-
{
74-
_cDataMode = cDataMode;
75-
return this;
76-
}
65+
/// <summary>
66+
/// Sets the <see cref="CDataMode"/> controlling how <c>message</c> and <c>exception</c> XML elements of log4net events are written.
67+
/// <para/>
68+
/// The default value is <see cref="Log4Net.CDataMode.Always"/>.
69+
/// </summary>
70+
/// <param name="cDataMode">The <see cref="CDataMode"/> to use.</param>
71+
/// <returns>The builder in order to fluently chain all options.</returns>
72+
public Log4NetTextFormatterOptionsBuilder UseCDataMode(CDataMode cDataMode)
73+
{
74+
_cDataMode = cDataMode;
75+
return this;
76+
}
7777

78-
/// <summary>
79-
/// Do not use any XML namespace for log4net events.
80-
/// <para/>
81-
/// The default value has prefix <c>log4net</c> and namespace <c>http://logging.apache.org/log4net/schemas/log4net-events-1.2/</c>.
82-
/// </summary>
83-
/// <returns>The builder in order to fluently chain all options.</returns>
84-
public Log4NetTextFormatterOptionsBuilder UseNoXmlNamespace()
85-
{
86-
_xmlNamespace = null;
87-
return this;
88-
}
78+
/// <summary>
79+
/// Do not use any XML namespace for log4net events.
80+
/// <para/>
81+
/// The default value has prefix <c>log4net</c> and namespace <c>http://logging.apache.org/log4net/schemas/log4net-events-1.2/</c>.
82+
/// </summary>
83+
/// <returns>The builder in order to fluently chain all options.</returns>
84+
public Log4NetTextFormatterOptionsBuilder UseNoXmlNamespace()
85+
{
86+
_xmlNamespace = null;
87+
return this;
88+
}
8989

90-
/// <summary>
91-
/// Sets the line ending used for log4net events.
92-
/// <para/>
93-
/// The default value is <see cref="Log4Net.LineEnding.LineFeed"/>.
94-
/// </summary>
95-
/// <param name="lineEnding">The <see cref="Log4Net.LineEnding"/> to use.</param>
96-
/// <returns>The builder in order to fluently chain all options.</returns>
97-
public Log4NetTextFormatterOptionsBuilder UseLineEnding(LineEnding lineEnding)
98-
{
99-
_lineEnding = lineEnding;
100-
return this;
101-
}
90+
/// <summary>
91+
/// Sets the line ending used for log4net events.
92+
/// <para/>
93+
/// The default value is <see cref="Log4Net.LineEnding.LineFeed"/>.
94+
/// </summary>
95+
/// <param name="lineEnding">The <see cref="Log4Net.LineEnding"/> to use.</param>
96+
/// <returns>The builder in order to fluently chain all options.</returns>
97+
public Log4NetTextFormatterOptionsBuilder UseLineEnding(LineEnding lineEnding)
98+
{
99+
_lineEnding = lineEnding;
100+
return this;
101+
}
102102

103-
/// <summary>
104-
/// Sets the indentation settings used for log4net events.
105-
/// <para/>
106-
/// The default value uses two spaces.
107-
/// </summary>
108-
/// <param name="indentationSettings">The <see cref="IndentationSettings"/> to use.</param>
109-
/// <returns>The builder in order to fluently chain all options.</returns>
110-
public Log4NetTextFormatterOptionsBuilder UseIndentationSettings(IndentationSettings indentationSettings)
111-
{
112-
_indentationSettings = indentationSettings;
113-
return this;
114-
}
103+
/// <summary>
104+
/// Sets the indentation settings used for log4net events.
105+
/// <para/>
106+
/// The default value uses two spaces.
107+
/// </summary>
108+
/// <param name="indentationSettings">The <see cref="IndentationSettings"/> to use.</param>
109+
/// <returns>The builder in order to fluently chain all options.</returns>
110+
public Log4NetTextFormatterOptionsBuilder UseIndentationSettings(IndentationSettings indentationSettings)
111+
{
112+
_indentationSettings = indentationSettings;
113+
return this;
114+
}
115115

116-
/// <summary>
117-
/// Do not indent log4net events.
118-
/// </summary>
119-
/// <returns>The builder in order to fluently chain all options.</returns>
120-
public Log4NetTextFormatterOptionsBuilder UseNoIndentation()
121-
{
122-
_indentationSettings = null;
123-
return this;
124-
}
116+
/// <summary>
117+
/// Do not indent log4net events.
118+
/// </summary>
119+
/// <returns>The builder in order to fluently chain all options.</returns>
120+
public Log4NetTextFormatterOptionsBuilder UseNoIndentation()
121+
{
122+
_indentationSettings = null;
123+
return this;
124+
}
125125

126-
/// <summary>
127-
/// Sets the <see cref="PropertyFilter"/> applied on all Serilog properties.
128-
/// <para/>
129-
/// The default property filter always returns <c>true</c>, i.e. it doesn't filter out any property.
130-
/// </summary>
131-
/// <remarks>If an exception is thrown while executing the filter, the default filter will be applied, i.e. the Serilog property will be included in the log4net properties.</remarks>
132-
/// <returns>The builder in order to fluently chain all options.</returns>
133-
public Log4NetTextFormatterOptionsBuilder UsePropertyFilter(PropertyFilter filterProperty)
134-
{
135-
_filterProperty = filterProperty ?? throw new ArgumentNullException(nameof(filterProperty), "The property filter can not be null.");
136-
return this;
137-
}
126+
/// <summary>
127+
/// Sets the <see cref="PropertyFilter"/> applied on all Serilog properties.
128+
/// <para/>
129+
/// The default property filter always returns <c>true</c>, i.e. it doesn't filter out any property.
130+
/// </summary>
131+
/// <remarks>If an exception is thrown while executing the filter, the default filter will be applied, i.e. the Serilog property will be included in the log4net properties.</remarks>
132+
/// <returns>The builder in order to fluently chain all options.</returns>
133+
public Log4NetTextFormatterOptionsBuilder UsePropertyFilter(PropertyFilter filterProperty)
134+
{
135+
_filterProperty = filterProperty ?? throw new ArgumentNullException(nameof(filterProperty), "The property filter can not be null.");
136+
return this;
137+
}
138138

139-
/// <summary>
140-
/// Sets the <see cref="ExceptionFormatter"/> controlling how all exceptions are formatted.
141-
/// <para/>
142-
/// If the formatter returns <see langword="null"/>, the exception will not be written to the log4net event.
143-
/// <para/>
144-
/// The default exception formatter calls <c>Exception.ToString()</c>.
145-
/// </summary>
146-
/// <remarks>If an exception is thrown while executing the formatter, the default formatter will be used, i.e. <c>Exception.ToString()</c>.</remarks>
147-
/// <returns>The builder in order to fluently chain all options.</returns>
148-
public Log4NetTextFormatterOptionsBuilder UseExceptionFormatter(ExceptionFormatter formatException)
149-
{
150-
_formatException = formatException ?? throw new ArgumentNullException(nameof(formatException), "The exception formatter can not be null.");
151-
return this;
152-
}
139+
/// <summary>
140+
/// Sets the <see cref="ExceptionFormatter"/> controlling how all exceptions are formatted.
141+
/// <para/>
142+
/// If the formatter returns <see langword="null"/>, the exception will not be written to the log4net event.
143+
/// <para/>
144+
/// The default exception formatter calls <c>Exception.ToString()</c>.
145+
/// </summary>
146+
/// <remarks>If an exception is thrown while executing the formatter, the default formatter will be used, i.e. <c>Exception.ToString()</c>.</remarks>
147+
/// <returns>The builder in order to fluently chain all options.</returns>
148+
public Log4NetTextFormatterOptionsBuilder UseExceptionFormatter(ExceptionFormatter formatException)
149+
{
150+
_formatException = formatException ?? throw new ArgumentNullException(nameof(formatException), "The exception formatter can not be null.");
151+
return this;
152+
}
153153

154-
/// <summary>
155-
/// Enables log4j compatibility mode. This tweaks the XML elements to match the log4j logging event specification.
156-
/// The DTD can be found at https://raw.githubusercontent.com/apache/log4j/v1_2_17/src/main/resources/org/apache/log4j/xml/log4j.dtd
157-
/// <para>
158-
/// Here is the list of differences between the log4net and the log4j XML layout:
159-
/// <list type="bullet">
160-
/// <item>The <c>event</c> elements have <c>log4j</c> instead of <c>log4net</c> XML namespace.</item>
161-
/// <item>The <c>timestamp</c> attribute uses milliseconds elapsed from 1/1/1970 instead of an ISO 8601 formatted date.</item>
162-
/// <item>The exception element is named <c>throwable</c> instead of <c>exception</c>.</item>
163-
/// </list>
164-
/// </para>
165-
/// </summary>
166-
/// <remarks>You must not change other options after calling this method.</remarks>
167-
public void UseLog4JCompatibility()
168-
{
169-
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L135
170-
_lineEnding = LineEnding.CarriageReturn | LineEnding.LineFeed;
154+
/// <summary>
155+
/// Enables log4j compatibility mode. This tweaks the XML elements to match the log4j logging event specification.
156+
/// The DTD can be found at https://raw.githubusercontent.com/apache/log4j/v1_2_17/src/main/resources/org/apache/log4j/xml/log4j.dtd
157+
/// <para>
158+
/// Here is the list of differences between the log4net and the log4j XML layout:
159+
/// <list type="bullet">
160+
/// <item>The <c>event</c> elements have <c>log4j</c> instead of <c>log4net</c> XML namespace.</item>
161+
/// <item>The <c>timestamp</c> attribute uses milliseconds elapsed from 1/1/1970 instead of an ISO 8601 formatted date.</item>
162+
/// <item>The exception element is named <c>throwable</c> instead of <c>exception</c>.</item>
163+
/// </list>
164+
/// </para>
165+
/// </summary>
166+
/// <remarks>You must not change other options after calling this method.</remarks>
167+
public void UseLog4JCompatibility()
168+
{
169+
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L135
170+
_lineEnding = LineEnding.CarriageReturn | LineEnding.LineFeed;
171171

172-
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137
173-
_xmlNamespace = Log4JXmlNamespace;
172+
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L137
173+
_xmlNamespace = Log4JXmlNamespace;
174174

175-
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L147
176-
_cDataMode = CDataMode.Always;
177-
}
175+
// https://github.com/apache/log4j/blob/v1_2_17/src/main/java/org/apache/log4j/xml/XMLLayout.java#L147
176+
_cDataMode = CDataMode.Always;
177+
}
178178

179-
internal Log4NetTextFormatterOptions Build()
180-
=> new(_formatProvider, _cDataMode, _xmlNamespace, CreateXmlWriterSettings(_lineEnding, _indentationSettings), _filterProperty, _formatException);
179+
internal Log4NetTextFormatterOptions Build()
180+
=> new(_formatProvider, _cDataMode, _xmlNamespace, CreateXmlWriterSettings(_lineEnding, _indentationSettings), _filterProperty, _formatException);
181181

182-
private static XmlWriterSettings CreateXmlWriterSettings(LineEnding lineEnding, IndentationSettings? indentationSettings)
182+
private static XmlWriterSettings CreateXmlWriterSettings(LineEnding lineEnding, IndentationSettings? indentationSettings)
183+
{
184+
var xmlWriterSettings = new XmlWriterSettings
185+
{
186+
Indent = indentationSettings is not null,
187+
NewLineChars = lineEnding.ToCharacters(),
188+
ConformanceLevel = ConformanceLevel.Fragment,
189+
};
190+
if (indentationSettings is not null)
183191
{
184-
var xmlWriterSettings = new XmlWriterSettings
185-
{
186-
Indent = indentationSettings is not null,
187-
NewLineChars = lineEnding.ToCharacters(),
188-
ConformanceLevel = ConformanceLevel.Fragment,
189-
};
190-
if (indentationSettings is not null)
191-
{
192-
xmlWriterSettings.IndentChars = indentationSettings.ToString();
193-
}
194-
return xmlWriterSettings;
192+
xmlWriterSettings.IndentChars = indentationSettings.ToString();
195193
}
194+
return xmlWriterSettings;
196195
}
197-
198-
/// <summary>
199-
/// Represents the method that determines whether a Serilog property must be included in the log4net properties.
200-
/// </summary>
201-
/// <param name="logEvent">The <see cref="LogEvent"/> associated with the Serilog property.</param>
202-
/// <param name="propertyName">The Serilog property name.</param>
203-
/// <returns><see langword="true"/> to include the Serilog property in the log4net properties or <see langword="false"/> to ignore the Serilog property.</returns>
204-
public delegate bool PropertyFilter(LogEvent logEvent, string propertyName);
205-
206-
/// <summary>
207-
/// Represents the method that formats an <see cref="Exception"/>.
208-
/// </summary>
209-
/// <param name="exception">The exception to be formatted.</param>
210-
public delegate string ExceptionFormatter(Exception exception);
211-
}
196+
}
197+
198+
/// <summary>
199+
/// Represents the method that determines whether a Serilog property must be included in the log4net properties.
200+
/// </summary>
201+
/// <param name="logEvent">The <see cref="LogEvent"/> associated with the Serilog property.</param>
202+
/// <param name="propertyName">The Serilog property name.</param>
203+
/// <returns><see langword="true"/> to include the Serilog property in the log4net properties or <see langword="false"/> to ignore the Serilog property.</returns>
204+
public delegate bool PropertyFilter(LogEvent logEvent, string propertyName);
205+
206+
/// <summary>
207+
/// Represents the method that formats an <see cref="Exception"/>.
208+
/// </summary>
209+
/// <param name="exception">The exception to be formatted.</param>
210+
public delegate string ExceptionFormatter(Exception exception);

0 commit comments

Comments
 (0)