Skip to content

Commit 1064764

Browse files
committed
completed OutputDebugStringAppenderTest
1 parent 5640c18 commit 1064764

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/log4net.Tests/Appender/OutputDebugAppenderTest.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020

2121
using System;
22-
using System.Diagnostics;
23-
2422
using log4net.Appender;
2523
using log4net.Config;
2624
using log4net.Core;
@@ -33,22 +31,21 @@ namespace log4net.Tests.Appender;
3331
/// <summary>
3432
/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/> class.
3533
/// </summary>
36-
/// <remarks>
37-
/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/> class.
38-
/// </remarks>
3934
[TestFixture]
4035
[Platform(Include = "Win")]
4136
public sealed class OutputDebugStringAppenderTest
4237
{
38+
private const string DebugMessage = "Message - Сообщение - הודעה";
39+
4340
/// <summary>
4441
/// Verifies that the OutputDebugString api is called by the appender without issues
4542
/// </summary>
4643
[Test]
4744
public void AppendShouldNotCauseAnyErrors()
4845
{
4946
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
50-
51-
OutputDebugStringAppender outputDebugStringAppender = new()
47+
string? lastDebugString = null;
48+
OutputAppender outputDebugStringAppender = new(value => lastDebugString = value)
5249
{
5350
Layout = new SimpleLayout(),
5451
ErrorHandler = new FailOnError()
@@ -58,15 +55,18 @@ public void AppendShouldNotCauseAnyErrors()
5855
BasicConfigurator.Configure(rep, outputDebugStringAppender);
5956

6057
ILog log = LogManager.GetLogger(rep.Name, GetType());
61-
log.Debug("Message - Сообщение - הודעה");
62-
63-
// need a way to check that the api is actually called and the string is properly marshalled.
58+
log.Debug(DebugMessage);
59+
Assert.That(lastDebugString, Is.Not.Null.And.Contains(DebugMessage));
6460
}
6561
}
6662

67-
class FailOnError : IErrorHandler
63+
file sealed class OutputAppender(Action<string> outputDebugString)
64+
: OutputDebugStringAppender(outputDebugString)
65+
{ }
66+
67+
file sealed class FailOnError : IErrorHandler
6868
{
6969
public void Error(string message, Exception? e, ErrorCode errorCode) => Assert.Fail($"Unexpected error: {message} exception: {e}, errorCode: {errorCode}");
7070
public void Error(string message, Exception e) => Assert.Fail($"Unexpected error: {message} exception: {e}");
71-
public void Error(string message) => Assert.Fail($"Unexpected error: {message}");
72-
}
71+
public void Error(string message) => Assert.Fail($"Unexpected error: {message}");
72+
}

src/log4net/Appender/OutputDebugStringAppender.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,33 @@
1717
//
1818
#endregion
1919

20-
using System.Runtime.InteropServices;
21-
20+
using System;
2221
using log4net.Core;
2322
using log4net.Util;
2423

2524
namespace log4net.Appender;
2625

2726
/// <summary>
28-
/// Appends log events to the OutputDebugString system.
27+
/// Appends log events to the OutputDebugString system
2928
/// </summary>
3029
/// <author>Nicko Cadell</author>
3130
/// <author>Gert Driesen</author>
3231
public class OutputDebugStringAppender : AppenderSkeleton
3332
{
33+
private readonly Action<string> _outputDebugString;
34+
35+
/// <inheritdoc/>
36+
public OutputDebugStringAppender()
37+
: this(null)
38+
{ }
39+
40+
/// <summary>
41+
/// Constructor for unit testing
42+
/// </summary>
43+
/// <param name="outputDebugString">replacement for <see cref="NativeMethods.OutputDebugString"/></param>
44+
protected OutputDebugStringAppender(Action<string>? outputDebugString)
45+
=> _outputDebugString = outputDebugString ?? NativeMethods.OutputDebugString;
46+
3447
/// <summary>
3548
/// Writes the logging event to the output debug string API
3649
/// </summary>
@@ -40,13 +53,13 @@ public class OutputDebugStringAppender : AppenderSkeleton
4053
protected override void Append(LoggingEvent loggingEvent)
4154
{
4255
#if NETSTANDARD2_0_OR_GREATER
43-
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
56+
if (!System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
4457
{
45-
throw new System.PlatformNotSupportedException("OutputDebugString is only available on Windows");
58+
throw new PlatformNotSupportedException("OutputDebugString is only available on Windows");
4659
}
4760
#endif
4861

49-
NativeMethods.OutputDebugString(RenderLoggingEvent(loggingEvent));
62+
_outputDebugString(RenderLoggingEvent(loggingEvent));
5063
}
5164

5265
/// <summary>

0 commit comments

Comments
 (0)