Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/changelog/3.2.1/268-shorten-test-output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="268" link="https://github.com/apache/logging-log4net/pull/268"/>
<description format="asciidoc">
shorten console output for unit tests (by @FreeAndNil in https://github.com/apache/logging-log4net/pull/268[#268])
</description>
</entry>
27 changes: 23 additions & 4 deletions src/log4net.Tests/Util/LogLogTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace log4net.Tests.Util;
[TestFixture]
public class LogLogTest
{
/// <summary>
/// Tests the <see cref="TraceListener"/> functionality
/// </summary>
[Test]
public void TraceListenerCounterTest()
{
Expand All @@ -46,6 +49,9 @@ public void TraceListenerCounterTest()
Assert.That(listTraceListener.Count, Is.EqualTo(2));
}

/// <summary>
/// Tests the <see cref="LogLog.EmitInternalMessages"/> property
/// </summary>
[Test]
public void EmitInternalMessages()
{
Expand All @@ -71,6 +77,9 @@ public void EmitInternalMessages()
}
}

/// <summary>
/// Tests the <see cref="LogLog.LogReceivedAdapter"/> class.
/// </summary>
[Test]
public void LogReceivedAdapter()
{
Expand All @@ -90,10 +99,13 @@ public void LogReceivedAdapter()
[Test]
public void LogReceivedAdapterThreading()
{
for (int i = 0; i < 1000; i++)
LogLog.ExecuteWithoutEmittingInternalMessages(() =>
{
LogReceivedAdapterThreadingCore(i);
}
for (int i = 0; i < 1000; i++)
{
LogReceivedAdapterThreadingCore(i);
}
});
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5394:Do not use insecure randomness",
Expand All @@ -119,13 +131,20 @@ private void LogReceivedAdapterThreadingCore(int seed)
}
}

/// <summary>
/// Mock for <see cref="TraceListener"/> that counts the number of calls to <see cref="Write(string?)"/>
/// </summary>
internal sealed class TraceListenerCounter : TraceListener
{
/// <inheritdoc/>
public override void Write(string? message) => Count++;

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

/// <inheritdoc/>
public void Reset() => Count = 0;

/// <inheritdoc/>
public int Count { get; private set; }
}
}
21 changes: 20 additions & 1 deletion src/log4net/Util/LogLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,29 @@ static LogLog()
public static bool QuietMode { get; set; }

/// <summary>
///
/// Configures whether internal messages are emitted to Console.Out and Console.Error.
/// </summary>
public static bool EmitInternalMessages { get; set; } = true;

/// <summary>
/// Execute the callback with internal messages suppressed
/// </summary>
/// <param name="callback">Callback</param>
public static void ExecuteWithoutEmittingInternalMessages(Action callback)
{
callback.EnsureNotNull();
bool oldEmitInternalMessages = EmitInternalMessages;
EmitInternalMessages = false;
try
{
callback();
}
finally
{
EmitInternalMessages = oldEmitInternalMessages;
}
}

/// <summary>
/// Raises the LogReceived event when an internal messages is received.
/// </summary>
Expand Down
Loading