Skip to content

Commit

Permalink
fixes #134 IndexOutOfRangeException in LogLog.LogReceived (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
FreeAndNil authored Mar 28, 2024
1 parent 67a2d86 commit 6f03114
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
60 changes: 42 additions & 18 deletions src/log4net.Tests/Util/LogLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
*
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using log4net.Util;
using NUnit.Framework;

Expand Down Expand Up @@ -73,33 +76,54 @@ public void LogReceivedAdapter()
{
var messages = new List<LogLog>();

using (new LogLog.LogReceivedAdapter(messages))
{
LogLog.Debug(GetType(), "Won't be recorded");
LogLog.Error(GetType(), "This will be recorded.");
LogLog.Error(GetType(), "This will be recorded.");
}
using var _ = new LogLog.LogReceivedAdapter(messages);
LogLog.Debug(GetType(), "Won't be recorded");
LogLog.Error(GetType(), "This will be recorded.");
LogLog.Error(GetType(), "This will be recorded.");

Assert.AreEqual(2, messages.Count);
}
}

public class TraceListenerCounter : TraceListener
{
public override void Write(string? message)

/// <summary>
/// Tests multi threaded calls to <see cref="LogLog.OnLogReceived"/>
/// </summary>
[Test]
public void LogReceivedAdapterThreading()
{
Count++;
for (int i = 0; i < 1000; i++)
{
LogReceivedAdapterThreadingCore(i);
}
}

public override void WriteLine(string? message)
private void LogReceivedAdapterThreadingCore(int seed)
{
Write(message);
var messages = new List<LogLog>(1);
var syncRoot = ((ICollection)messages).SyncRoot;
var random = new Random(seed);
using var _ = new LogLog.LogReceivedAdapter(messages);
Parallel.For(0, 10, i =>
{
if (random.Next(10) > 8)
{
lock (syncRoot)
{
messages.Clear();
messages.Capacity = 1;
}
}
LogLog.Warn(GetType(), messages.Capacity.ToString() + ' ' + messages.Count);
});
}
}

public void Reset()
{
Count = 0;
}
internal sealed class TraceListenerCounter : TraceListener
{
public override void Write(string? message) => Count++;

public override void WriteLine(string? message) => Write(message);

public void Reset() => Count = 0;

public int Count { get; private set; }
}
Expand Down
10 changes: 5 additions & 5 deletions src/log4net/Util/LogLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ public sealed class LogLog
/// sent to Console.Out and Trace.Write.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Prefix + Source.Name + ": " + Message;
}
public override string ToString() => $"{Prefix}{Source.Name}: {Message}";

/// <summary>
/// Initializes a new instance of the <see cref="LogLog" /> class.
Expand Down Expand Up @@ -522,7 +519,10 @@ public LogReceivedAdapter(List<LogLog> items)

void LogLog_LogReceived(object? source, LogReceivedEventArgs e)
{
Items.Add(e.LogLog);
lock (((ICollection)Items).SyncRoot)
{
Items.Add(e.LogLog);
}
}

/// <summary>
Expand Down

0 comments on commit 6f03114

Please sign in to comment.