This is an automated email from the ASF dual-hosted git repository.
freeandnil pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4net.git
The following commit(s) were added to refs/heads/master by this push:
new 10647645 completed OutputDebugStringAppenderTest
10647645 is described below
commit 10647645458a0ccce9ebbc5b0fa9b6b42b55332d
Author: Jan Friedrich <[email protected]>
AuthorDate: Mon Dec 15 15:43:46 2025 +0100
completed OutputDebugStringAppenderTest
---
.../Appender/OutputDebugAppenderTest.cs | 26 +++++++++++-----------
src/log4net/Appender/OutputDebugStringAppender.cs | 25 ++++++++++++++++-----
2 files changed, 32 insertions(+), 19 deletions(-)
diff --git a/src/log4net.Tests/Appender/OutputDebugAppenderTest.cs
b/src/log4net.Tests/Appender/OutputDebugAppenderTest.cs
index 9659244c..882b1d53 100644
--- a/src/log4net.Tests/Appender/OutputDebugAppenderTest.cs
+++ b/src/log4net.Tests/Appender/OutputDebugAppenderTest.cs
@@ -19,8 +19,6 @@
using System;
-using System.Diagnostics;
-
using log4net.Appender;
using log4net.Config;
using log4net.Core;
@@ -33,13 +31,12 @@ namespace log4net.Tests.Appender;
/// <summary>
/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/>
class.
/// </summary>
-/// <remarks>
-/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/>
class.
-/// </remarks>
[TestFixture]
[Platform(Include = "Win")]
public sealed class OutputDebugStringAppenderTest
{
+ private const string DebugMessage = "Message - Сообщение - הודעה";
+
/// <summary>
/// Verifies that the OutputDebugString api is called by the appender
without issues
/// </summary>
@@ -47,8 +44,8 @@ public sealed class OutputDebugStringAppenderTest
public void AppendShouldNotCauseAnyErrors()
{
ILoggerRepository rep =
LogManager.CreateRepository(Guid.NewGuid().ToString());
-
- OutputDebugStringAppender outputDebugStringAppender = new()
+ string? lastDebugString = null;
+ OutputAppender outputDebugStringAppender = new(value => lastDebugString =
value)
{
Layout = new SimpleLayout(),
ErrorHandler = new FailOnError()
@@ -58,15 +55,18 @@ public void AppendShouldNotCauseAnyErrors()
BasicConfigurator.Configure(rep, outputDebugStringAppender);
ILog log = LogManager.GetLogger(rep.Name, GetType());
- log.Debug("Message - Сообщение - הודעה");
-
- // need a way to check that the api is actually called and the string is
properly marshalled.
+ log.Debug(DebugMessage);
+ Assert.That(lastDebugString, Is.Not.Null.And.Contains(DebugMessage));
}
}
-class FailOnError : IErrorHandler
+file sealed class OutputAppender(Action<string> outputDebugString)
+ : OutputDebugStringAppender(outputDebugString)
+{ }
+
+file sealed class FailOnError : IErrorHandler
{
public void Error(string message, Exception? e, ErrorCode errorCode) =>
Assert.Fail($"Unexpected error: {message} exception: {e}, errorCode:
{errorCode}");
public void Error(string message, Exception e) => Assert.Fail($"Unexpected
error: {message} exception: {e}");
- public void Error(string message) => Assert.Fail($"Unexpected error:
{message}");
-}
+ public void Error(string message) => Assert.Fail($"Unexpected error:
{message}");
+}
\ No newline at end of file
diff --git a/src/log4net/Appender/OutputDebugStringAppender.cs
b/src/log4net/Appender/OutputDebugStringAppender.cs
index 83e83da7..44442e9d 100644
--- a/src/log4net/Appender/OutputDebugStringAppender.cs
+++ b/src/log4net/Appender/OutputDebugStringAppender.cs
@@ -17,20 +17,33 @@
//
#endregion
-using System.Runtime.InteropServices;
-
+using System;
using log4net.Core;
using log4net.Util;
namespace log4net.Appender;
/// <summary>
-/// Appends log events to the OutputDebugString system.
+/// Appends log events to the OutputDebugString system
/// </summary>
/// <author>Nicko Cadell</author>
/// <author>Gert Driesen</author>
public class OutputDebugStringAppender : AppenderSkeleton
{
+ private readonly Action<string> _outputDebugString;
+
+ /// <inheritdoc/>
+ public OutputDebugStringAppender()
+ : this(null)
+ { }
+
+ /// <summary>
+ /// Constructor for unit testing
+ /// </summary>
+ /// <param name="outputDebugString">replacement for <see
cref="NativeMethods.OutputDebugString"/></param>
+ protected OutputDebugStringAppender(Action<string>? outputDebugString)
+ => _outputDebugString = outputDebugString ??
NativeMethods.OutputDebugString;
+
/// <summary>
/// Writes the logging event to the output debug string API
/// </summary>
@@ -40,13 +53,13 @@ public class OutputDebugStringAppender : AppenderSkeleton
protected override void Append(LoggingEvent loggingEvent)
{
#if NETSTANDARD2_0_OR_GREATER
- if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ if
(!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
{
- throw new System.PlatformNotSupportedException("OutputDebugString is
only available on Windows");
+ throw new PlatformNotSupportedException("OutputDebugString is only
available on Windows");
}
#endif
- NativeMethods.OutputDebugString(RenderLoggingEvent(loggingEvent));
+ _outputDebugString(RenderLoggingEvent(loggingEvent));
}
/// <summary>