-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#194 fixed a bug in the Dispose-Logic of TelnetAppender
- Loading branch information
1 parent
8f3c3d4
commit fb8b42f
Showing
4 changed files
with
209 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace log4net.Tests.Appender.Internal | ||
{ | ||
/// <summary> | ||
/// Telnet Client for unit testing | ||
/// </summary> | ||
/// <param name="received">Callback for received messages</param> | ||
/// <param name="port">TCP-Port to use</param> | ||
internal sealed class SimpleTelnetClient( | ||
Action<string> received, int port) : IDisposable | ||
{ | ||
private readonly CancellationTokenSource cancellationTokenSource = new(); | ||
private readonly TcpClient client = new(); | ||
|
||
/// <summary> | ||
/// Runs the client (in a task) | ||
/// </summary> | ||
internal void Run() => Task.Run(() => | ||
{ | ||
client.Connect(new IPEndPoint(IPAddress.Loopback, port)); | ||
// Get a stream object for reading and writing | ||
using NetworkStream stream = client.GetStream(); | ||
int i; | ||
byte[] bytes = new byte[256]; | ||
// Loop to receive all the data sent by the server | ||
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) | ||
{ | ||
string data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); | ||
received(data); | ||
if (cancellationTokenSource.Token.IsCancellationRequested) | ||
{ | ||
return; | ||
} | ||
} | ||
}, cancellationTokenSource.Token); | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() | ||
{ | ||
cancellationTokenSource.Cancel(); | ||
cancellationTokenSource.Dispose(); | ||
client.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#region Apache License | ||
// | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to you under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
#endregion | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Xml; | ||
using log4net.Appender; | ||
using log4net.Config; | ||
using log4net.Core; | ||
using log4net.Repository; | ||
using log4net.Tests.Appender.Internal; | ||
using NUnit.Framework; | ||
|
||
namespace log4net.Tests.Appender; | ||
|
||
/// <summary> | ||
/// Tests for <see cref="TelnetAppender"/> | ||
/// </summary> | ||
[TestFixture] | ||
public sealed class TelnetAppenderTest | ||
{ | ||
/// <summary> | ||
/// Simple Test für the <see cref="TelnetAppender"/> | ||
/// </summary> | ||
/// <remarks> | ||
/// https://github.com/apache/logging-log4net/issues/194 | ||
/// https://stackoverflow.com/questions/79053363/log4net-telnetappender-doesnt-work-after-migrate-to-log4net-3 | ||
/// </remarks> | ||
[Test] | ||
public void TelnetTest() | ||
{ | ||
List<string> received = []; | ||
|
||
XmlDocument log4netConfig = new(); | ||
int port = 9090; | ||
log4netConfig.LoadXml($""" | ||
<log4net> | ||
<appender name="TelnetAppender" type="log4net.Appender.TelnetAppender"> | ||
<port value="{port}" /> | ||
<layout type="log4net.Layout.PatternLayout"> | ||
<conversionPattern value="%date %-5level - %message%newline" /> | ||
</layout> | ||
</appender> | ||
<root> | ||
<level value="INFO"/> | ||
<appender-ref ref="TelnetAppender"/> | ||
</root> | ||
</log4net> | ||
"""); | ||
string logId = Guid.NewGuid().ToString(); | ||
ILoggerRepository repository = LogManager.CreateRepository(logId); | ||
XmlConfigurator.Configure(repository, log4netConfig["log4net"]!); | ||
using (SimpleTelnetClient telnetClient = new(Received, port)) | ||
{ | ||
telnetClient.Run(); | ||
WaitForReceived(1); // wait for welcome message | ||
ILogger logger = repository.GetLogger("Telnet"); | ||
logger.Log(typeof(TelnetAppenderTest), Level.Info, logId, null); | ||
WaitForReceived(2); // wait for log message | ||
} | ||
repository.Shutdown(); | ||
Assert.AreEqual(2, received.Count); | ||
StringAssert.Contains(logId, received[1]); | ||
|
||
void Received(string message) => received.Add(message); | ||
|
||
void WaitForReceived(int count) | ||
{ | ||
while (received.Count < count) | ||
{ | ||
Thread.Sleep(10); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.