Repository navigation
Description
Summary
src/log4net/Appender/IAppender.cs:
/// <summary>
/// Gets or sets the name of this appender.
/// </summary>
/// <value>The name of the appender.</value>
/// <remarks>
/// <para>The name uniquely identifies the appender.</para>
/// </remarks>
string Name { get; set; }Nothing in log4net makes that true. Name is a plain settable property, and
attaching appenders doesn't check it: AppenderAttachedImpl.AddAppender
dedupes by reference, not by name:
public void AddAppender(IAppender appender)
{
appender.EnsureNotNull();
_appenderArray = null;
_appenderList ??= new(1);
if (!_appenderList.Contains(appender))
{
_appenderList.Add(appender);
}
}So two appenders with the same Name can both be attached to the same logger.
When that happens, lookup silently picks the first one:
public IAppender? GetAppender(string? name)
{
if (_appenderList is not null && name is not null)
{
foreach (IAppender appender in _appenderList)
{
if (name == appender.Name)
{
return appender;
}
}
}
return null;
}and RemoveAppender(string name) is RemoveAppender(GetAppender(name)), so it
removes the first match and leaves the other attached.
Why it matters
The doc reads as a guarantee you can build on. "Name uniquely identifies the
appender" invites you to treat GetAppender(name) / RemoveAppender(name) as
addressing the appender. With a duplicated name (easy to produce from
programmatic configuration, or by assembling config from fragments) you get
appenders you can't reach or remove by name, and no warning anywhere. Stating
that the name is a label rather than a key sets the right expectation.
Suggested fix
/// <value>The name of the appender.</value>
/// <remarks>
- /// <para>The name uniquely identifies the appender.</para>
+ /// <para>Used to look the appender up; uniqueness is not enforced,
+ /// and lookup returns the first match.</para>
/// </remarks>
string Name { get; set; }and the implementation side, src/log4net/Appender/AppenderSkeleton.cs:
/// <summary>
- /// Gets or sets the name that uniquely identifies this appender.
+ /// Gets or sets the name of this appender.
/// </summary>
public string Name { get; set; } = string.Empty;Upstream reference
log4j 1.x had the same wording on Appender.getName() and corrected it under
Bug 43849 ("Javadoc for Appender.getName implies return value will be unique and
not null"), apache/logging-log4j1@e3fecab964 (2008-08-12):
--- a/src/main/java/org/apache/log4j/Appender.java
+++ b/src/main/java/org/apache/log4j/Appender.java
@@ -74,8 +74,8 @@ public interface Appender {
/**
- Get the name of this appender. The name uniquely identifies the
- appender. */
+ Get the name of this appender.
+ @return name, may be null.*/
public
String getName();The same commit also touched AppenderSkeleton:
--- a/src/main/java/org/apache/log4j/AppenderSkeleton.java
+++ b/src/main/java/org/apache/log4j/AppenderSkeleton.java
@@ -189,7 +189,8 @@ public abstract class AppenderSkeleton implements Appender, OptionHandler {
/**
- Returns the name of this FileAppender.
+ Returns the name of this appender.
+ @return name, may be null.
*/
public
finalOnly the uniqueness half of that carries over to log4net: Name here is
declared string Name { get; set; } in a nullable-annotated file, so "may be
null" would be wrong. Hence the suggested fix drops the uniqueness claim and
says what the name is actually good for, rather than copying log4j's text
verbatim.
log4net's implementation side has the same claim, so it's the same two files
log4j touched. src/log4net/Appender/AppenderSkeleton.cs additionally defaults
Name to string.Empty, which is another reason it can't be treated as an
identifier: two appenders that were never named both answer "".
Activity
Merge pull request #341 from apache/Feature/340-appender-name-not-unique