Skip to content

Commit 7fd03a2

Browse files
committed
Add Windows debug trace listener and update tracing
Introduced WindowsOutputDebugListener to enable OutputDebugString tracing on Windows platforms. Updated GeneralTracer to conditionally add this listener for Windows, improved stack trace handling, and included the new listener in relevant project files for compilation.
1 parent e5ce29d commit 7fd03a2

File tree

5 files changed

+41
-8
lines changed

5 files changed

+41
-8
lines changed

src/c#/GeneralUpdate.Bowl/GeneralUpdate.Bowl.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\AbstractStrategy.cs" Link="Common\AbstractStrategy.cs" />
7373
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\IStrategy.cs" Link="Common\IStrategy.cs" />
7474
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
75+
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
7576
<Compile Include="..\GeneralUpdate.Common\Shared\Object\Configinfo.cs" Link="Common\Configinfo.cs" />
7677
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\BaseResponseDTO.cs" Link="Common\BaseResponseDTO.cs" />
7778
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\VersionRespDTO.cs" Link="Common\VersionRespDTO.cs" />

src/c#/GeneralUpdate.ClientCore/GeneralUpdate.ClientCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\AbstractStrategy.cs" Link="Common\AbstractStrategy.cs" />
6363
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\IStrategy.cs" Link="Common\IStrategy.cs" />
6464
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
65+
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
6566
<Compile Include="..\GeneralUpdate.Common\Shared\Object\Configinfo.cs" Link="Common\Configinfo.cs" />
6667
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\BaseResponseDTO.cs" Link="Common\BaseResponseDTO.cs" />
6768
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\VersionRespDTO.cs" Link="Common\VersionRespDTO.cs" />

src/c#/GeneralUpdate.Common/Internal/Trace/GeneralTracer.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Runtime.InteropServices;
45

56
namespace GeneralUpdate.Common.Internal;
67

@@ -13,7 +14,14 @@ public static class GeneralTracer
1314
static GeneralTracer()
1415
{
1516
Trace.Listeners.Clear();
17+
18+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
19+
{
20+
Trace.Listeners.Add(new WindowsOutputDebugListener());
21+
}
22+
1623
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out) { Name = "ConsoleListener" });
24+
1725
InitializeFileListener();
1826

1927
if (Debugger.IsAttached)
@@ -91,26 +99,20 @@ private static void WriteTraceMessage(TraceLevel level, string message)
9199
var levelName = GetLevelName(level);
92100
var fullMessage = string.Empty;
93101

94-
#if !AOT
95102
try
96103
{
97104
var stackFrame = new StackFrame(2, true);
98105
var method = stackFrame.GetMethod();
99106
var className = method.DeclaringType?.Name ?? "UnknownType";
100107
var methodName = method.Name;
101108
var lineNumber = stackFrame.GetFileLineNumber();
102-
103109
var lineInfo = lineNumber > 0 ? $"Line {lineNumber}" : "Line N/A (Line numbers may not be displayed in Release mode)";
104110
fullMessage = $"[{timestamp}] [{levelName}] {className}.{methodName} ({lineInfo}): {message}";
105111
}
106-
catch (Exception ex)
112+
catch
107113
{
108-
fullMessage = $"[{timestamp}] [{levelName}] [Failed to obtain stack information: {ex.Message}] : {message}";
109-
}
110-
#endif
111-
112-
if (string.IsNullOrEmpty(fullMessage))
113114
fullMessage = $"[{timestamp}] [{levelName}] : {message}";
115+
}
114116

115117
lock (_lockObj)
116118
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
4+
5+
namespace GeneralUpdate.Common.Internal;
6+
7+
public class WindowsOutputDebugListener : TraceListener
8+
{
9+
// 声明Win32 API的OutputDebugString函数
10+
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
11+
private static extern void OutputDebugString(string lpOutputString);
12+
13+
// 重写Write方法(处理无换行的输出)
14+
public override void Write(string message)
15+
{
16+
if (!string.IsNullOrEmpty(message))
17+
{
18+
OutputDebugString(message);
19+
}
20+
}
21+
22+
// 重写WriteLine方法(处理带换行的输出)
23+
public override void WriteLine(string message)
24+
{
25+
// 附加换行符,保持与Trace.WriteLine一致的行为
26+
Write($"{message}{Environment.NewLine}");
27+
}
28+
}

src/c#/GeneralUpdate.Core/GeneralUpdate.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\AbstractStrategy.cs" Link="Common\AbstractStrategy.cs" />
6666
<Compile Include="..\GeneralUpdate.Common\Internal\Strategy\IStrategy.cs" Link="Common\IStrategy.cs" />
6767
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\GeneralTracer.cs" Link="Common\GeneralTracer.cs" />
68+
<Compile Include="..\GeneralUpdate.Common\Internal\Trace\WindowsOutputDebugListener.cs" Link="Common\WindowsOutputDebugListener.cs" />
6869
<Compile Include="..\GeneralUpdate.Common\Shared\Object\Configinfo.cs" Link="Common\Configinfo.cs" />
6970
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\BaseResponseDTO.cs" Link="Common\BaseResponseDTO.cs" />
7071
<Compile Include="..\GeneralUpdate.Common\Shared\Object\DTO\VersionRespDTO.cs" Link="Common\VersionRespDTO.cs" />

0 commit comments

Comments
 (0)