This is an automated email from the ASF dual-hosted git repository. freeandnil pushed a commit to branch Feature/TelnetBug in repository https://gitbox.apache.org/repos/asf/logging-log4net.git
commit 7ad3c2c09d40babb8d59e08c2d6b8a87ea91bbf2 Author: Jan Friedrich <[email protected]> AuthorDate: Sun Oct 13 13:58:08 2024 +0200 Test for telnet bug --- .../Appender/Internal/SimpleTelnetClient.cs | 34 ++++++++++ src/log4net.Tests/Appender/TelnetAppenderTest.cs | 74 ++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/log4net.Tests/Appender/Internal/SimpleTelnetClient.cs b/src/log4net.Tests/Appender/Internal/SimpleTelnetClient.cs new file mode 100644 index 00000000..8feddc49 --- /dev/null +++ b/src/log4net.Tests/Appender/Internal/SimpleTelnetClient.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Sockets; + +namespace log4net.Tests.Appender.Internal +{ + internal sealed class SimpleTelnetClient(Action<string> received, int port = 9090) + { + internal void Run() + { + TcpClient client = new("localhost", port); + try + { + // 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); + } + } + finally + { + client.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/src/log4net.Tests/Appender/TelnetAppenderTest.cs b/src/log4net.Tests/Appender/TelnetAppenderTest.cs new file mode 100644 index 00000000..626ae65b --- /dev/null +++ b/src/log4net.Tests/Appender/TelnetAppenderTest.cs @@ -0,0 +1,74 @@ +#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.Linq; +using System.Threading.Tasks; +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 class TelnetAppenderTest +{ + /// <summary> + /// https://stackoverflow.com/questions/79053363/log4net-telnetappender-doesnt-work-after-migrate-to-log4net-3 + /// </summary> + [Test] + public void TelnetTest() + { + List<string> received = []; + + XmlDocument log4netConfig = new(); + log4netConfig.LoadXml(""" + <log4net> + <appender name="TelnetAppender" type="log4net.Appender.TelnetAppender"> + <port value="9090" /> + <layout type="log4net.Layout.PatternLayout"> + <conversionPattern value="%date{HH:mm:ss.fff} %-5level - %message%newline" /> + </layout> + </appender> + <root> + <level value="INFO"/> + <appender-ref ref="TelnetAppender"/> + </root> + </log4net> +"""); + ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString()); + XmlConfigurator.Configure(rep, log4netConfig["log4net"]!); + Task task = Task.Run(() => new SimpleTelnetClient(Received).Run()); + task.Wait(500); + + rep.GetLogger("Telnet").Log(typeof(TelnetAppenderTest), Level.Info, "Log-Message", null); + + Assert.AreEqual(1, received.Count); + + void Received(string message) => received.Add(message); + } +} \ No newline at end of file
