Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/log4net/Repository/Hierarchy/Hierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,11 @@ public override void ResetConfiguration()
{
Root.Level = LevelMap.LookupWithDefault(Level.Debug);
Threshold = LevelMap.LookupWithDefault(Level.All);

// Reset the warning flag so diagnostics can fire during the next
// reconfiguration cycle. Without this, the "no appender" warning is
// silenced permanently after the first configuration.
EmittedNoAppenderWarning = false;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Shutdown(); // nested locks are OK

foreach (Logger logger in GetCurrentLoggers().OfType<Logger>())
Expand Down Expand Up @@ -775,4 +779,4 @@ public override string ToString()
/// </remarks>
internal void AddProperty(PropertyEntry propertyEntry)
=> Properties[propertyEntry.Key.EnsureNotNull()] = propertyEntry.EnsureNotNull().Value;
}
}
35 changes: 34 additions & 1 deletion src/log4net/Repository/Hierarchy/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,37 @@

CallAppenders(logEvent);
}
}

/// <summary>
/// Atomically replaces all appenders with the provided collection.
/// </summary>
/// <param name="appenders">The new set of appenders to attach.</param>
/// <remarks>
/// <para>
/// This method removes the existing appenders and attaches all new
/// appenders inside a single writer lock, minimizing the window
/// during which the logger has no appenders. This reduces silent log
/// event loss during reconfiguration.
/// </para>
/// </remarks>
public virtual void ReplaceAppenders(IEnumerable<IAppender> appenders)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (macos-14)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (macos-14)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (macos-14)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (macos-14)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 610 in src/log4net/Repository/Hierarchy/Logger.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04)

The type or namespace name 'IEnumerable<>' could not be found (are you missing a using directive or an assembly reference?)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please adjust the indentation

{
_appenderLock.AcquireWriterLock();
try
{
_appenderAttachedImpl?.RemoveAllAppenders();
_appenderAttachedImpl = null;

foreach (IAppender appender in appenders)
{
_appenderAttachedImpl ??= new();
_appenderAttachedImpl.AddAppender(appender);
}
}
finally
{
_appenderLock.ReleaseWriterLock();
}
}

}
59 changes: 37 additions & 22 deletions src/log4net/Repository/Hierarchy/XmlHierarchyConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,43 +389,58 @@ protected void ParseRoot(XmlElement rootElement)
/// <para>
/// Parse the child elements of a &lt;logger&gt; element.
/// </para>
/// <para>
/// Appenders are collected from XML first, then applied atomically via
/// <see cref="Logger.ReplaceAppenders"/>, minimizing the window during
/// which the logger has no appenders and silent log event loss can occur.
/// </para>
/// </remarks>
protected void ParseChildrenOfLoggerElement(XmlElement catElement, Logger log, bool isRoot)
{
// Remove all existing appenders from log. They will be
// reconstructed if need be.
log.EnsureNotNull().RemoveAllAppenders();
log.EnsureNotNull();
catElement.EnsureNotNull();

// Phase 1: resolve all new appenders from XML *before* touching the
// live logger. This avoids the window where the logger has no appenders.
var newAppenders = new List<IAppender>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var newAppenders = new List<IAppender>();
List<IAppender> newAppenders = new();


foreach (XmlNode currentNode in catElement.EnsureNotNull().ChildNodes)
foreach (XmlNode currentNode in catElement.ChildNodes)
{
if (currentNode.NodeType == XmlNodeType.Element)
if (currentNode.NodeType != XmlNodeType.Element)
{
XmlElement currentElement = (XmlElement)currentNode;
continue;
}

if (currentElement.LocalName == AppenderRefTag)
{
string refName = currentElement.GetAttribute(RefAttr);
if (FindAppenderByReference(currentElement) is IAppender appender)
{
LogLog.Debug(_declaringType, $"Adding appender named [{refName}] to logger [{log.Name}].");
log.AddAppender(appender);
}
else
{
LogLog.Error(_declaringType, $"Appender named [{refName}] not found.");
}
}
else if (currentElement.LocalName is LevelTag or PriorityTag)
XmlElement currentElement = (XmlElement)currentNode;

if (currentElement.LocalName == AppenderRefTag)
{
string refName = currentElement.GetAttribute(RefAttr);
if (FindAppenderByReference(currentElement) is IAppender appender)
{
ParseLevel(currentElement, log, isRoot);
LogLog.Debug(_declaringType, $"Resolved appender [{refName}] for logger [{log.Name}].");
newAppenders.Add(appender);
}
else
{
SetParameter(currentElement, log);
LogLog.Error(_declaringType, $"Appender named [{refName}] not found.");
}
}
else if (currentElement.LocalName is LevelTag or PriorityTag)
{
ParseLevel(currentElement, log, isRoot);
}
else
{
SetParameter(currentElement, log);
}
}

// Phase 2: atomic swap — replace all appenders in one writer lock so
// the logger is never in a zero-appender state for longer than it takes
// to acquire and release the lock (microseconds, not milliseconds).
log.ReplaceAppenders(newAppenders);

if (log is IOptionHandler optionHandler)
{
optionHandler.ActivateOptions();
Expand Down
Loading