Sysinternals的'如果在.NET 4下使用,DebugView不再有效。一些研究表明,如果连接了调试器,框架的新架构不允许捕获跟踪;就我而言,它是Visual Studio调试器。将目标框架从4更改为3.5使其再次起作用。
有人知道在连接Visual Studio调试器的同时让DebugView在.NET 4下工作的方法吗?我尝试清除Trace类的Listeners集合,但没有运气。
答案 0 :(得分:23)
使用Windows内核中的OutputDebugString
函数发出.NET跟踪消息。此功能,如MSDN中所述,
将字符串发送到调试器以供显示。
显然,本机调试器将收到此消息。这意味着remark这种行为是设计的。在.NET 4.0之前将消息传递给其他侦听器(如DebugView)的原因是Visual Studio没有将.NET代码调试为“本机”调试器;附加本机调试器后,DebugView从未工作过。
解决方法可能是添加TraceListener
,将所有邮件转发给未附加调试程序的其他进程。可以使用任何IPC机制来实现通信。以下是使用TCP套接字的示例。
服务器应用程序
这是一个简单的独立命令行程序,由TraceListener
类自动启动和停止:
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine("Usage: DebugOutputListener.exe <port>");
return;
}
TcpListener server = null;
try
{
Int32 port = Convert.ToInt32(args[0]);
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Start();
while (true)
{
Console.Write("Waiting for a connection... ");
using (TcpClient client = server.AcceptTcpClient())
{
using (NetworkStream stream = client.GetStream())
{
byte[] bufferLength = new byte[4];
stream.Read(bufferLength, 0, 4);
int length = BitConverter.ToInt32(bufferLength, 0);
if (length == -1)
{
// close message received
Trace.WriteLine("DebugOutputListener is closing.");
return;
}
byte[] bufferMessage = new byte[length];
stream.Read(bufferMessage, 0, length);
string msg = Encoding.UTF8.GetString(bufferMessage);
Trace.WriteLine(msg);
}
}
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
}
finally
{
server.Stop();
}
}
}
<强>的TraceListener 强>
using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class DebugOutputTraceListener : TraceListener
{
private IPEndPoint ipEndPoint;
private bool needsDisposing;
public DebugOutputTraceListener(string debugOutputListenerPath, int port)
{
this.ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000);
// start the process that forwards the trace messages
var psi = new ProcessStartInfo()
{
FileName = debugOutputListenerPath,
Arguments = port.ToString(),
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(psi);
needsDisposing = true;
}
~DebugOutputTraceListener()
{
Dispose(false);
}
public override void Write(string message)
{
sendMessage(message);
}
public override void WriteLine(string message)
{
sendMessage(message + Environment.NewLine);
}
private void sendMessage(string message)
{
try
{
using (TcpClient client = new TcpClient())
{
client.Connect(ipEndPoint);
byte[] bufferMessage = Encoding.UTF8.GetBytes(message);
byte[] bufferLength =
BitConverter.GetBytes(bufferMessage.Length);
using (NetworkStream stream = client.GetStream())
{
stream.Write(bufferLength, 0, bufferLength.Length);
stream.Write(bufferMessage, 0, bufferMessage.Length);
}
}
}
catch (SocketException e)
{
Trace.WriteLine(e.ToString());
}
}
/// <summary>
/// Sends -1 to close the TCP listener server.
/// </summary>
private void sendCloseMessage()
{
try
{
using (TcpClient client = new TcpClient())
{
client.Connect(ipEndPoint);
byte[] buffer = BitConverter.GetBytes(-1);
using (NetworkStream stream = client.GetStream())
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
catch (SocketException e)
{
Trace.WriteLine(e.ToString());
}
}
public override void Close()
{
sendCloseMessage();
needsDisposing = false;
base.Close();
}
protected override void Dispose(bool disposing)
{
if (needsDisposing)
{
sendCloseMessage();
needsDisposing = false;
}
base.Dispose(disposing);
}
}
<强>用法强>
public class Program
{
[STAThread]
static void Main(string[] args)
{
// using Debug; start a listener process on port 13000
Debug.Listeners.Add(
new DebugOutputTraceListener("DebugOutputListener.exe", 13000));
Debug.WriteLine("A debug message.");
// using Trace; start a listener process on port 13001
Trace.Listeners.Add(
new DebugOutputTraceListener("DebugOutputListener.exe", 13001));
Trace.WriteLine("A trace message");
}
}
答案 1 :(得分:17)
根据您的需要,有一个更简单的解决方法:只需使用Ctrl-F5即可在没有调试器的情况下启动应用程序。
我原本希望使用DebugView从托管的Silverlight应用程序中捕获调试语句,该应用程序在调试器中不起作用。虽然这不像在.NET 4之前那样工作,但是在没有调试的情况下启动我的主机会让调试器语句通过并显示在DebugView中。
答案 2 :(得分:6)
这解决了我的问题:
Trace.Autoflush = true;
答案 3 :(得分:2)
当我将一些项目从.NET 4.5降级到.NET 4时,我遇到了这个问题 - 突然我的所有Debug View数据都消失了(我直接PInvoking到:: OutputDebugString)。无论如何,升级到最新可用版本的Debug View(4.81)解决了这个问题。