如何使DebugView在.NET 4下工作?

问题描述 投票:28回答:4

SysInternals的DebugView如果在.NET 4下使用就不再工作了。一些研究表明,如果连接了调试器,框架的新架构不允许捕获痕迹;在我的例子中,它是Visual Studio调试器。把目标框架从4改成3.5,又可以工作了。

有谁知道有什么方法可以让DebugView在连接了Visual Studio调试器的情况下在.NET 4下工作?我试过清除Trace类的Listeners集合,但没有成功。

.net debugging .net-4.0 trace debugview
4个回答
23
投票

.NET的跟踪消息是使用的。OutputDebugString 函数,在Windows内核中。这个函数,在MSDN里有记载。

发送一个字符串给调试器显示。

显然,本地调试器会收到这个消息。这是由 评论 这种行为是设计出来的。在.NET 4.0之前,这些消息被传递给其他监听器(如DebugView)的原因是Visual Studio没有将.NET代码作为 "原生 "调试器进行调试;当附加了原生调试器时,DebugView从未工作过。

一个变通的方法是在Visual Studio中添加一个 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");
    }
}

17
投票

根据你的需求,有一个更简单的变通方法:只需使用Ctrl-F5启动你的应用程序而不使用调试器。

我曾希望使用DebugView从托管的Silverlight应用程序中捕获调试语句,但在调试器中无法工作。虽然这不能像在.NET 4之前那样工作,但在不调试的情况下启动我的主机确实可以让调试器的语句通过,并且它们显示在DebugView中。


6
投票

这为我解决了这个问题。

Trace.AutoFlush = true;

2
投票

当我把一些项目从.NET 4.5降级到.NET 4时,我遇到了这个问题--突然间我所有的Debug View数据都消失了(我直接PInvoking到::OutputDebugString)。无论如何,升级到最新的Debug View版本(4.81)后,这个问题就解决了。

© www.soinside.com 2019 - 2024. All rights reserved.