Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make copy of appender list when removing all, for increased thread-safety #222

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 5 additions & 17 deletions src/log4net/Util/AppenderAttachedImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ public int AppendLoopOnAppenders(LoggingEvent loggingEvent)
return 0;
}

_appenderArray ??= _appenderList.ToArray();

foreach (IAppender appender in _appenderArray)
foreach (IAppender appender in _appenderList.ToArray())
{
try
{
Expand Down Expand Up @@ -116,9 +114,7 @@ public int AppendLoopOnAppenders(LoggingEvent[] loggingEvents)
return 0;
}

_appenderArray ??= _appenderList.ToArray();

foreach (IAppender appender in _appenderArray)
foreach (IAppender appender in _appenderList.ToArray())
{
try
{
Expand Down Expand Up @@ -173,7 +169,6 @@ private static void CallAppend(IAppender appender, LoggingEvent[] loggingEvents)
public void AddAppender(IAppender appender)
{
appender.EnsureNotNull();
_appenderArray = null;
_appenderList ??= new(1);
if (!_appenderList.Contains(appender))
{
Expand Down Expand Up @@ -223,7 +218,7 @@ public AppenderCollection Appenders
{
if (_appenderList is not null && name is not null)
{
foreach (IAppender appender in _appenderList)
foreach (IAppender appender in _appenderList.ToArray())
{
if (name == appender.Name)
{
Expand All @@ -246,7 +241,7 @@ public void RemoveAllAppenders()
{
if (_appenderList is not null)
{
foreach (IAppender appender in _appenderList)
foreach (IAppender appender in _appenderList.ToArray())
{
try
{
Expand All @@ -258,7 +253,6 @@ public void RemoveAllAppenders()
}
}
_appenderList = null;
_appenderArray = null;
}
}

Expand All @@ -283,7 +277,6 @@ public void RemoveAllAppenders()
{
_appenderList = null;
}
_appenderArray = null;
}
return appender;
}
Expand All @@ -307,11 +300,6 @@ public void RemoveAllAppenders()
/// </summary>
private AppenderCollection? _appenderList;

/// <summary>
/// Array of appenders, used to cache the appenderList
/// </summary>
private IAppender[]? _appenderArray;

/// <summary>
/// The fully qualified type of the AppenderAttachedImpl class.
/// </summary>
Expand All @@ -320,4 +308,4 @@ public void RemoveAllAppenders()
/// log message.
/// </remarks>
private static readonly Type _declaringType = typeof(AppenderAttachedImpl);
}
}