簡體   English   中英

在代碼中啟動調試器

[英]Start Debugger in Code

我需要調試從一鍵安裝啟動的應用程序。 (VS 2010,帶有 Office 7 的 Excel VSTO)。 根據提供給一鍵安裝程序應用程序的登錄憑據,用戶應該會看到兩個啟動頁面之一。 這在我的機器上一切正常,但是在部署時,從默認頁面更改為第二個啟動頁面會導致錯誤。

對於我的生活,我無法弄清楚如何從 VS2010 中調試該過程。 我可以在輸入憑據之前附加到登錄名,但我無法附加到 Excel,因為它在我單擊“確定”按鈕之前不會啟動。

那么,有沒有辦法讓 Excel 或者更確切地說,我的代碼在實例化時調用調試器,以便我可以弄清楚為什么我的圖像資源在部署的應用程序中不可用?

謝謝。

蘭迪

System.Diagnostics.Debugger.Launch();

最簡單

要從代碼使用強制斷點:

if (System.Diagnostics.Debugger.IsAttached)
    System.Diagnostics.Debugger.Break();

當應用程序未在 Visual Studio 中啟動時(包括遠程調試)

有時應用程序無法從 Visual Studio 啟動,但必須進行調試。 如果 Visual Studio 正在運行,我會使用此代碼檢查應用程序內部的表單,並提供將其附加到 Visual Studio 的可能性。

using System.Diagnostics;

....

// get debugger processes
Process[] procName1 = Process.GetProcessesByName("devenv");

// get remote debugging processes
Process[] procName2 = Process.GetProcessesByName("msvsmon"); 

// If Visual Studio or remote debug are running halt the application by showing a MessageBox and give opportunity to attach the debugger
if (procName1.Length > 0 || procName2.Length > 0)
{
    if (MessageBox.Show(Application.Current.MainWindow, "Force breakpoint?", "Wait for debugger attach", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
    {
        // Force a breakpoint when the debugger became attached
        if (System.Diagnostics.Debugger.IsAttached)
            System.Diagnostics.Debugger.Break(); // force a breakpoint
    }
}

今天我需要一個控制台應用程序的解決方案。 這里是:

using System.Diagnostics;

....

// Wait for debugger attach and force breakpoint
// for a console app
//
if (!System.Diagnostics.Debugger.IsAttached) // not needed when debugger is already attached
{
    /// for "eternal" wait (~ 68 years) use: 
    ///     int waitTimeout = int.MaxValue 
    int waitTimeout = 60; 

    // get debugger processes
    Process[] procName1 = Process.GetProcessesByName("devenv");

    // get remote debugging processes
    Process[] procName2 = Process.GetProcessesByName("msvsmon");

    // If Visual Studio or remote debug are running halt the application by showing an message and give opportunity to attach the debugger
    if (procName1.Length > 0 || procName2.Length > 0)
    {
        while (true)
        {
            Console.WriteLine("Visual studio is running | Force breakpoint? (Attach the debugger before pressing any key!)");
            Console.WriteLine("[Y]es, [No]");

            DateTime beginWait = DateTime.Now;
            while (!Console.KeyAvailable && DateTime.Now.Subtract(beginWait).TotalSeconds < waitTimeout)
            {
                Thread.Sleep(250); // sleep 1/4 second
            }

            if (!Console.KeyAvailable)
            {
                break; // timeout elapsed without any kepress
                //<----------
            }

            var key = Console.ReadKey(false);
            if (key.Key == ConsoleKey.Y)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break(); // force a breakpoint
                    break; // leave the while(true)
                    //<----------
                }
                else
                {
                    Console.WriteLine("No debugger attached");
                    Console.WriteLine("");
                }
            }
            else if (key.Key == ConsoleKey.N)
            {
                break; // leave the while(true)
                //<----------
            }
            Console.WriteLine("");
        }
    }
}

如果您安裝了 Visual Studio,Juan 的回答是最好的。 但是如果目標機器沒有它,您可能需要進行某種暫停(我通常會將一個對話框作為 main 中的第一件事,讓它等待我附加)然后使用遠程調試器附加到它在你的機器上

如果它運行的時間足夠長,您可以附加到 Excel,但嚴重的是我懷疑錯誤是否存在。

您可以附加到正在運行的應用程序/進程,如果符號可用(調試版本),您就可以真正進行調試,但應用程序必須存活足夠長的時間才能選擇它進行附加。

我認為,從您所說的來看,您需要的是正確的異常和錯誤日志記錄,例如 Log4Net 或 NLog 之類的東西,它可以在每個異常中存儲所有內容(堆棧跟蹤、異常詳細信息...),這樣您就可以清楚地識別出真正的問題是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM