Skip to content

Commit 794413f

Browse files
committed
shorten console output for unit tests
1 parent e61c425 commit 794413f

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="268" link="https://github.com/apache/logging-log4net/pull/268"/>
7+
<description format="asciidoc">
8+
shorten console output for unit tests (by @FreeAndNil in https://github.com/apache/logging-log4net/pull/268[#268])
9+
</description>
10+
</entry>

src/log4net.Tests/Util/LogLogTest.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace log4net.Tests.Util;
3232
[TestFixture]
3333
public class LogLogTest
3434
{
35+
/// <summary>
36+
/// Tests the <see cref="TraceListener"/> functionality
37+
/// </summary>
3538
[Test]
3639
public void TraceListenerCounterTest()
3740
{
@@ -46,6 +49,9 @@ public void TraceListenerCounterTest()
4649
Assert.That(listTraceListener.Count, Is.EqualTo(2));
4750
}
4851

52+
/// <summary>
53+
/// Tests the <see cref="LogLog.EmitInternalMessages"/> property
54+
/// </summary>
4955
[Test]
5056
public void EmitInternalMessages()
5157
{
@@ -71,6 +77,9 @@ public void EmitInternalMessages()
7177
}
7278
}
7379

80+
/// <summary>
81+
/// Tests the <see cref="LogLog.LogReceivedAdapter"/> class.
82+
/// </summary>
7483
[Test]
7584
public void LogReceivedAdapter()
7685
{
@@ -90,10 +99,13 @@ public void LogReceivedAdapter()
9099
[Test]
91100
public void LogReceivedAdapterThreading()
92101
{
93-
for (int i = 0; i < 1000; i++)
102+
LogLog.ExecuteWithoutEmittingInternalMessages(() =>
94103
{
95-
LogReceivedAdapterThreadingCore(i);
96-
}
104+
for (int i = 0; i < 1000; i++)
105+
{
106+
LogReceivedAdapterThreadingCore(i);
107+
}
108+
});
97109
}
98110

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

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

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

145+
/// <inheritdoc/>
128146
public void Reset() => Count = 0;
129147

148+
/// <inheritdoc/>
130149
public int Count { get; private set; }
131-
}
150+
}

src/log4net/Util/LogLog.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,29 @@ static LogLog()
223223
public static bool QuietMode { get; set; }
224224

225225
/// <summary>
226-
///
226+
/// Configures whether internal messages are emitted to Console.Out and Console.Error.
227227
/// </summary>
228228
public static bool EmitInternalMessages { get; set; } = true;
229229

230+
/// <summary>
231+
/// Execute the callback with internal messages suppressed
232+
/// </summary>
233+
/// <param name="callback">Callback</param>
234+
public static void ExecuteWithoutEmittingInternalMessages(Action callback)
235+
{
236+
callback.EnsureNotNull();
237+
bool oldEmitInternalMessages = EmitInternalMessages;
238+
EmitInternalMessages = false;
239+
try
240+
{
241+
callback();
242+
}
243+
finally
244+
{
245+
EmitInternalMessages = oldEmitInternalMessages;
246+
}
247+
}
248+
230249
/// <summary>
231250
/// Raises the LogReceived event when an internal messages is received.
232251
/// </summary>

0 commit comments

Comments
 (0)