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
6 changes: 5 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ almost always be doing.
`[CallerArgumentExpression(nameof(value))]`, so no argument name is passed at the call site.
This includes constructor and property assignments: write `_x = x.EnsureNotNull();`,
not `_x = x ?? throw new ArgumentNullException(nameof(x));`.
- `SystemInfo.CreateArgumentOutOfRangeException(value, message)` names the parameter the same way:
pass the value itself, never a name.
- Appenders never let exceptions escape to the caller. The house pattern is
`catch (Exception e) when (!e.IsFatal()) { ErrorHandler.Error("...", e); }`.
- **A catch of `Exception` always carries `when (!e.IsFatal())`.** The filter is not a licence to
Expand Down Expand Up @@ -154,7 +156,9 @@ almost always be doing.
an expanded `params` argument trips CS8620 in a C# 14 extension block, which
`WarningsAsErrors=nullable` makes a build error.
- **NUnit constructs one fixture instance for the whole fixture**, so an instance field that
records what a test observed accumulates across the tests in it. Clear such state in `[SetUp]`.
records what a test observed accumulates across the tests in it. Clear such state in `[SetUp]`,
or give the fixture `[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]`, as
`BackgroundSenderTest` does.
- Drive a test over a background thread with gates (`ManualResetEventSlim`), never with
`Thread.Sleep`: park the worker, assert the state you care about, then release it. See
`BackgroundSenderTest`, where every wait has a generous timeout and the assertions are exact.
Expand Down
11 changes: 11 additions & 0 deletions src/changelog/3.6.1/370-close-summary-once.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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="370" link="https://github.com/apache/logging-log4net/issues/370"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">Closing `TelnetAppender`, `RemoteSyslogAppender` or the `SmtpAppender` of
`log4net.Ext.Mail` reports the total number of messages not sent once, instead of twice
(fixed by @FreeAndNil)</description>
</entry>
12 changes: 12 additions & 0 deletions src/changelog/3.6.1/370-full-queue-waits-once.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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="370" link="https://github.com/apache/logging-log4net/issues/370"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">When the send queue of `TelnetAppender`, `RemoteSyslogAppender` or the
`SmtpAppender` of `log4net.Ext.Mail` is full, a logging call waits `EnqueueTimeoutMillis` once; later
calls drop at once until a send succeeds, instead of each waiting the full timeout
(ANT-2026-M13HDM0A, fixed by @FreeAndNil)</description>
</entry>
11 changes: 11 additions & 0 deletions src/changelog/3.6.1/371-icmp-error-costs-no-record.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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="371" link="https://github.com/apache/logging-log4net/issues/371"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">`RemoteSyslogAppender` sends a record once more when the send reports a
socket error, which on Linux may belong to the datagram before it. A record is no longer lost to the
ICMP error of the one before it; the error is reported (ANT-2026-C2Q2AQZL, fixed by @FreeAndNil)</description>
</entry>
11 changes: 11 additions & 0 deletions src/changelog/3.6.1/372-argument-range-name-from-caller.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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="changed">
<issue id="372" link="https://github.com/apache/logging-log4net/issues/372"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">`SystemInfo.CreateArgumentOutOfRangeException` takes the parameter name
from the expression passed as the value and is hidden from IntelliSense. Code compiled against an
older log4net that calls it has to be rebuilt (implemented by @FreeAndNil)</description>
</entry>
11 changes: 11 additions & 0 deletions src/changelog/3.6.1/372-other-line-breaks-escaped.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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="372" link="https://github.com/apache/logging-log4net/issues/372"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">`LocalSyslogAppender` with `NewLineHandling` `Escape` or `Split` writes
VT, FF, FS, GS, RS, NEL, U+2028 and U+2029 as `\uXXXX`, like CR and LF, so a reader that breaks lines
there no longer shows one record as two (ANT-2026-1YRNCBP9, fixed by @FreeAndNil)</description>
</entry>
12 changes: 12 additions & 0 deletions src/changelog/3.6.1/372-undefined-newline-handling-rejected.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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="372" link="https://github.com/apache/logging-log4net/issues/372"/>
<issue id="373" link="https://github.com/apache/logging-log4net/pull/373"/>
<description format="asciidoc">`LocalSyslogAppender` rejects a `NewLineHandling` other than `Escape`,
`Split` or `Keep` on activation, as `RemoteSyslogAppender` does, so the appender logs nothing. A
configuration with a number such as `5` ran as `Keep` until now, with raw line breaks
(ANT-2026-6O92ZO65, fixed by @FreeAndNil)</description>
</entry>
7 changes: 4 additions & 3 deletions src/log4net.Ext.Mail.Tests/Appender/SmtpAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public void SendFailureIsReportedToTheErrorHandlerAndNotThrown()

Assert.DoesNotThrow(() => Append(appender));

Assert.That(_errorHandler.Message, Does.Contain("Failed to send a logging event."));
Assert.That(_errorHandler.Message, Does.Contain("Failed to send a message."));
Assert.That(_errorHandler.Message, Does.Contain("relay refused"));
}

Expand Down Expand Up @@ -869,7 +869,7 @@ public void TheDeadlineStopsASendThatDoesNotFinish()
Append(appender);

Assert.That(_transport.SentMails, Is.Empty);
Assert.That(_errorHandler.Message, Does.Contain("Failed to send a logging event."));
Assert.That(_errorHandler.Message, Does.Contain("Failed to send a message."));
Assert.That(_transport.CancelledCalls, Is.EqualTo(new[] { "Send" }));
}

Expand All @@ -888,7 +888,8 @@ public void EnqueueTimeoutMillisDefaultsTo5000()
[TestCase(-1)]
public void SendQueueSizeRejectsValuesThatAreNotPositive(int value)
=> Assert.That(() => CreateAppender().SendQueueSize = value,
Throws.TypeOf<ArgumentOutOfRangeException>());
Throws.TypeOf<ArgumentOutOfRangeException>()
.With.Property(nameof(ArgumentOutOfRangeException.ParamName)).EqualTo("value"));

/// <summary>0 is allowed and never waits, a negative wait is meaningless.</summary>
[Test]
Expand Down
13 changes: 8 additions & 5 deletions src/log4net.Ext.Mail/Appender/SmtpAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public int MaxBodySize
{
if (value < MailBodies.MinBodySize)
{
throw SystemInfo.CreateArgumentOutOfRangeException(nameof(value), value,
throw SystemInfo.CreateArgumentOutOfRangeException(value,
$"The value specified is less than {MailBodies.MinBodySize}.");
}
field = value;
Expand Down Expand Up @@ -439,7 +439,7 @@ public int SendQueueSize
{
if (value <= 0)
{
throw SystemInfo.CreateArgumentOutOfRangeException(nameof(value), value,
throw SystemInfo.CreateArgumentOutOfRangeException(value,
"The value specified for SendQueueSize is not positive.");
}
_sendQueueSize = value;
Expand All @@ -449,6 +449,9 @@ public int SendQueueSize
/// <summary>
/// How long a logging call waits for room in a full queue. Defaults to 5000, 0 never waits.
/// </summary>
/// <remarks>
/// Once a wait timed out, logging calls drop without waiting until a send succeeds.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">The value specified is negative.</exception>
public int EnqueueTimeoutMillis
{
Expand All @@ -457,7 +460,7 @@ public int EnqueueTimeoutMillis
{
if (value < 0)
{
throw SystemInfo.CreateArgumentOutOfRangeException(nameof(value), value,
throw SystemInfo.CreateArgumentOutOfRangeException(value,
"The value specified for EnqueueTimeoutMillis is negative.");
}
_enqueueTimeoutMillis = value;
Expand Down Expand Up @@ -517,7 +520,7 @@ private void Report(string message, Exception? exception)
SmtpTransportSecurity.ImplicitTls => SecureSocketOptions.SslOnConnect,
SmtpTransportSecurity.StartTls => SecureSocketOptions.StartTls,
SmtpTransportSecurity.StartTlsWhenAvailable => SecureSocketOptions.StartTlsWhenAvailable,
_ => throw SystemInfo.CreateArgumentOutOfRangeException(nameof(TransportSecurity), TransportSecurity,
_ => throw SystemInfo.CreateArgumentOutOfRangeException(TransportSecurity,
$"The value specified for TransportSecurity is not one of the {nameof(SmtpTransportSecurity)} values.")
};

Expand Down Expand Up @@ -572,7 +575,7 @@ public int SendTimeoutMillis
{
if (value <= 0)
{
throw SystemInfo.CreateArgumentOutOfRangeException(nameof(value), value,
throw SystemInfo.CreateArgumentOutOfRangeException(value,
"The value specified for SendTimeoutMillis is not positive.");
}
_sendTimeoutMillis = value;
Expand Down
13 changes: 13 additions & 0 deletions src/log4net.Tests/Appender/AppenderCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,17 @@ public void ReadOnlyToArrayTest()
Assert.That(appenderArray, Has.Length.EqualTo(1));
Assert.That(appenderArray[0], Is.EqualTo(appender));
}

/// <summary>
/// An index out of range is reported under the name of the indexer's parameter.
/// </summary>
[Test]
public void AnIndexOutOfRangeNamesTheIndex()
{
AppenderCollection appenderCollection = [];

Assert.That(() => appenderCollection[-1],
Throws.TypeOf<System.ArgumentOutOfRangeException>()
.With.Property(nameof(System.ArgumentOutOfRangeException.ParamName)).EqualTo("index"));
}
}
10 changes: 10 additions & 0 deletions src/log4net.Tests/Appender/Internal/UdpMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ internal sealed class UdpMock : IUdpConnection
/// </summary>
internal Exception? ConnectException { get; set; }

/// <summary>
/// Each send takes the next of these, if any, and throws it instead of sending.
/// </summary>
internal Queue<Exception> SendExceptions { get; } = new();

/// <inheritdoc/>
public void Connect(int localPort, IPAddress host, int remotePort)
{
Expand All @@ -66,6 +71,11 @@ public void Connect(int localPort, IPAddress host, int remotePort)
/// <inheritdoc/>
public Task<int> SendAsync(byte[] datagram, int bytes)
{
if (SendExceptions.Count > 0)
{
throw SendExceptions.Dequeue();
}

Sent.Add((datagram, bytes));
return Task.FromResult(bytes);
}
Expand Down
94 changes: 94 additions & 0 deletions src/log4net.Tests/Appender/LocalSyslogAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,100 @@ public void NewLineHandlingDefaultsToEscape()
=> Assert.That(new LocalSyslogAppender().NewLineHandling,
Is.EqualTo(SyslogNewLineHandling.Escape));

/// <summary>
/// <c>newLineHandling</c> binds through <see cref="Enum.Parse(Type, string)"/>, which accepts any number, so an
/// undefined value must not turn the escaping off unnoticed.
/// </summary>
[TestCase(3)]
[TestCase(5)]
[TestCase(-1)]
public void AnUndefinedNewLineHandlingIsRejected(int value)
{
LocalSyslogAppender appender = new() { NewLineHandling = (SyslogNewLineHandling)value };

Assert.That(appender.ActivateOptions, Throws.TypeOf<ArgumentOutOfRangeException>());
}

/// <summary>Each defined value passes the check.</summary>
[TestCase(SyslogNewLineHandling.Keep)]
[TestCase(SyslogNewLineHandling.Split)]
[TestCase(SyslogNewLineHandling.Escape)]
[Platform("Linux")]
[NonParallelizable]
public void EveryDefinedNewLineHandlingIsAccepted(SyslogNewLineHandling value)
{
LocalSyslogAppender appender = new()
{
NewLineHandling = value,
Identity = "log4net-test",
Layout = new PatternLayout("%message")
};

Assert.That(appender.ActivateOptions, Throws.Nothing);
appender.Close();
}

/// <summary>An undefined value set after activation escapes, as the default does.</summary>
[Test]
public void AnUndefinedNewLineHandlingEscapes()
{
LocalSyslogAppender appender = new() { NewLineHandling = (SyslogNewLineHandling)5 };

Assert.That(GetRecords(appender, "a\nb"), Is.EqualTo(new[] { "a\\nb" }));
}

/// <summary><see cref="SyslogNewLineHandling.Keep"/> passes the newlines through.</summary>
[Test]
public void KeepLeavesTheNewLines()
{
LocalSyslogAppender appender = new() { NewLineHandling = SyslogNewLineHandling.Keep };

Assert.That(GetRecords(appender, "a\nb"), Is.EqualTo(new[] { "a\nb" }));
}

/// <summary>
/// A reader may break a line at these too, so <see cref="SyslogNewLineHandling.Escape"/> and
/// <see cref="SyslogNewLineHandling.Split"/> escape them; <see cref="SyslogNewLineHandling.Keep"/> passes them.
/// </summary>
[TestCase("a\vb", "a\\u000bb", TestName = "OtherLineBreaksAreEscapedUnlessKept(VT)")]
[TestCase("a\fb", "a\\u000cb", TestName = "OtherLineBreaksAreEscapedUnlessKept(FF)")]
[TestCase("a\u001cb", "a\\u001cb", TestName = "OtherLineBreaksAreEscapedUnlessKept(FS)")]
[TestCase("a\u001db", "a\\u001db", TestName = "OtherLineBreaksAreEscapedUnlessKept(GS)")]
[TestCase("a\u001eb", "a\\u001eb", TestName = "OtherLineBreaksAreEscapedUnlessKept(RS)")]
[TestCase("a\u2028b", "a\\u2028b", TestName = "OtherLineBreaksAreEscapedUnlessKept(LS)")]
[TestCase("a\u2029b", "a\\u2029b", TestName = "OtherLineBreaksAreEscapedUnlessKept(PS)")]
[TestCase("a\u0085b", "a\\u0085b", TestName = "OtherLineBreaksAreEscapedUnlessKept(NEL)")]
public void OtherLineBreaksAreEscapedUnlessKept(string message, string expected)
{
Assert.That(GetRecords(new() { NewLineHandling = SyslogNewLineHandling.Escape }, message),
Is.EqualTo(new[] { expected }));
Assert.That(GetRecords(new() { NewLineHandling = SyslogNewLineHandling.Split }, message),
Is.EqualTo(new[] { expected }));
Assert.That(GetRecords(new() { NewLineHandling = SyslogNewLineHandling.Keep }, message),
Is.EqualTo(new[] { message }));
}

/// <summary><see cref="SyslogNewLineHandling.Split"/> escapes every line, not only the first.</summary>
[Test]
public void SplitEscapesEveryLine()
=> Assert.That(GetRecords(new() { NewLineHandling = SyslogNewLineHandling.Split }, "a\vb\nc\u2028d"),
Is.EqualTo(new[] { "a\\u000bb", "c\\u2028d" }));

/// <summary>The escape comes before the cut, so the escaped record still fits the limit.</summary>
[Test]
public void OtherLineBreaksAreEscapedBeforeTheCut()
{
LocalSyslogAppender appender = new() { MaxRecordSize = 512, ErrorHandler = new RecordingErrorHandler() };

Assert.That(GetRecords(appender, new string('\v', 100)),
Is.EqualTo(new[] { string.Concat(Enumerable.Repeat("\\u000b", 83)) + "...[truncated]" }));
}

/// <summary>Tab and text outside ASCII are no line breaks.</summary>
[Test]
public void TabAndNonAsciiTextAreUnchanged()
=> Assert.That(GetRecords(new(), "a\tb \u00e4\u3042"), Is.EqualTo(new[] { "a\tb \u00e4\u3042" }));

/// <summary>One record per line, and a blank line is no record at all.</summary>
[Test]
public void SplittingDropsTheEmptyLines()
Expand Down
Loading
Loading