Skip to content

Commit 8d678e1

Browse files
authored
Fix marshalling for OutputDebugString parameter, add unit test (#271)
* Fix DllImport attribute for OutputDebugString function * Fix encoding issue, add test * Run OutputDebugStringAppenderTest on Windows only
1 parent 6cb49cd commit 8d678e1

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#region Apache License
2+
//
3+
// Licensed to the Apache Software Foundation (ASF) under one or more
4+
// contributor license agreements. See the NOTICE file distributed with
5+
// this work for additional information regarding copyright ownership.
6+
// The ASF licenses this file to you under the Apache License, Version 2.0
7+
// (the "License"); you may not use this file except in compliance with
8+
// the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#endregion
19+
20+
21+
using System;
22+
using System.Diagnostics;
23+
24+
using log4net.Appender;
25+
using log4net.Config;
26+
using log4net.Core;
27+
using log4net.Layout;
28+
using log4net.Repository;
29+
using NUnit.Framework;
30+
31+
namespace log4net.Tests.Appender;
32+
33+
/// <summary>
34+
/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/> class.
35+
/// </summary>
36+
/// <remarks>
37+
/// Used for internal unit testing the <see cref="OutputDebugStringAppender"/> class.
38+
/// </remarks>
39+
[TestFixture]
40+
[Platform(Include = "Win")]
41+
public sealed class OutputDebugStringAppenderTest
42+
{
43+
/// <summary>
44+
/// Verifies that the OutputDebugString api is called by the appender without issues
45+
/// </summary>
46+
[Test]
47+
public void AppendShouldNotCauseAnyErrors()
48+
{
49+
ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
50+
51+
OutputDebugStringAppender outputDebugStringAppender = new()
52+
{
53+
Layout = new SimpleLayout(),
54+
ErrorHandler = new FailOnError()
55+
};
56+
outputDebugStringAppender.ActivateOptions();
57+
58+
BasicConfigurator.Configure(rep, outputDebugStringAppender);
59+
60+
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.
64+
}
65+
}
66+
67+
class FailOnError : IErrorHandler
68+
{
69+
public void Error(string message, Exception? e, ErrorCode errorCode) => Assert.Fail($"Unexpected error: {message} exception: {e}, errorCode: {errorCode}");
70+
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+
}

src/log4net/Util/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ internal static extern int FormatMessage(
139139
/// Stub for OutputDebugString native method
140140
/// </summary>
141141
/// <param name="message">the string to output</param>
142-
[DllImport("Kernel32.dll")]
142+
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
143143
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
144144
internal static extern void OutputDebugString(string message);
145145

0 commit comments

Comments
 (0)