Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#194 fixed a bug in the Dispose-Logic of TelnetAppender #195

Merged
merged 1 commit into from
Oct 17, 2024
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
4 changes: 2 additions & 2 deletions scripts/build-preview.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$Version = '3.0.1'
$Preview = '2'
$Version = '3.0.2'
$Preview = '1'
'building ...'
dotnet build -c Release "-p:GeneratePackages=true;PackageVersion=$Version-preview.$Preview" $PSScriptRoot/../src/log4net/log4net.csproj
'signing ...'
Expand Down
52 changes: 52 additions & 0 deletions src/log4net.Tests/Appender/Internal/SimpleTelnetClient.cs
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();
}
}
}
92 changes: 92 additions & 0 deletions src/log4net.Tests/Appender/TelnetAppenderTest.cs
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);
}
}
}
}
Loading