[英]Can output from OutputDebugString be viewed in Visual Studio's output window?
我正在使用 C# 和 Visual Studio 2010。
當我使用OutputDebugString
寫入調試信息時,它是否應該顯示在 output window 中?
我可以在DebugView的OutputDebugString
中看到 output,但我想我會在 Visual Studio 的 Output window 中看到它。 我在菜單工具下查看過? 選項? 調試? 一般,並且 output 不會被重定向到即時 window。 我還查看了菜單工具*? 選項? 調試? Output Window和所有常規 Output 設置設置為“開”。 最后,我使用了 Output window 中的下拉列表來指定應該出現調試消息。
如果我更改菜單工具*? 選項? 調試? 一般要將 output 重定向到即時 window, OutputDebugString
消息不會出現在即時 window 中。
這是我的整個測試程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace OutputDebugString
{
class Program
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern void OutputDebugString(string message);
static void Main(string[] args)
{
Console.WriteLine("Main - Enter - Console.WriteLine");
Debug.WriteLine("Main - Enter - Debug.WriteLine");
OutputDebugString("Main - Enter - OutputDebugString");
OutputDebugString("Main - Exit - OutputDebugString");
Debug.WriteLine("Main - Exit - Debug.WriteLine");
Console.WriteLine("Main - Exit - Console.WriteLine");
}
}
}
If I run within the debugger, the Debug.WriteLine
output does show up in the output window, but the OutputDebugString
output does not.
如果我從控制台 window 運行,DebugView 中會同時顯示Debug.WriteLine
和OutputDebugString
。
為什么OutputDebugString
output 從未出現在 output window 中?
最終,我的意圖不是用OutputDebugString
編寫大量調試 output ,而是使用 System.Diagnostics 或 NLog 或類似的東西。 我只是想知道,如果我將日志平台配置為寫入OutputDebugString
,output 在調試器中是否可見。
我回到我的原始程序(不是上面的簡單測試),它使用通過app.config
文件配置的TraceSources
和TraceListeners
。 如果我將跟蹤源配置為寫入System.Diagnostics.DefaultTraceListener
(記錄為寫入OutputDebugString
),那么跟蹤源 output DOES go 到調試 Z05B8C35CBD962FBF2427C。 但是,直接使用OutputDebugString
寫入的行(例如在我的簡單示例中)不會 go 到調試 window。 此外,如果我使用不同的TraceListener
寫入OutputDebugString
(我從 Codeplex 的 Ukadc.Diagnostics 獲得了一個),則 output 不會 go 到調試 Z05B8C74CBD9706FFBF2F4DE4C1。
關於 Ukadc.Diagnostics 跟蹤偵聽器的注釋... Ukadc.Diagnostics 包含一些跟蹤偵聽器,允許自定義 output 格式(類似於 log4net、NLog 和 LAB 中可用的格式)。 因此,“僅”依賴於 Ukadc.Diagnostics 可以使用“標准”.NET 診斷日志記錄,但我可以獲得一些高級功能(如 output 格式),而無需依賴可能更大的平台。 在這種情況下,我可以使用 Ukadc.Diagnostics OutputDebugStringTraceListener
將日志記錄 output 以與寫入文件時相同的格式(如果需要,或不同的格式)寫入調試 window。
請注意,我已經看到了這些問題,但它們沒有提供有效的解決方案:
你讓我在這個問題上問了一段時間。 沒門! 道路。
項目 > 屬性 > 調試選項卡,打開“啟用非托管代碼調試”復選框。 在更高的 VS 版本中重命名為“啟用本機代碼調試”。 啟用非托管代碼調試引擎后,OutputDebugString() 輸出現在可以正確攔截並定向到輸出窗口。
調試時(調試 => 開始調試 F5),設置項目 + 屬性,調試選項卡,選中“啟用非托管代碼調試”效果很好。
當不調試時(調試 => 開始而不調試 CTRL+F5),您必須使用 SysInternals 庫中的 DebugView。 下載 DebugView for Windows v4.76
由於設置,它可能會顯示在“立即窗口”中:
或者類似的東西。
我有項目 > 屬性 > 調試選項卡 > 勾選了“啟用非托管代碼調試”,但即使斷點起作用, OutputDebugString()
也不起作用。
我也有解決方案 > 通用屬性 > 項目依賴 > 在我的 C# 項目下勾選了我的 DLL,但構建依賴項不起作用。
解決這兩個問題的方法是解決方案> 配置屬性> 在我的 DLL 上勾選構建。
我曾經遇到過這個問題,並且該線程中建議的所有解決方案都失敗了。 DebugView 能夠看到調試消息,而 Visual Studio 2017 卻不能,無論我如何嘗試配置它。 啟用本機代碼調試? 查看。 沒有重定向到“立即”? 查看。 項目沒有從編譯中排除? 查看。
最終,我在安裝時使用“修復”選項通過 Visual Studio 安裝程序解決了問題。 花了大約10分鍾完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.