visual-studio 将监视的 Visual Studio 变量输出到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1333317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output a watched Visual Studio variable to a file
提问by Unsliced
Is there a way in Visual Studio (2008 if it matters) that I can, in debug/break mode, write the contents of a variable to a text/XML file?
在 Visual Studio(2008,如果重要的话)中有没有办法在调试/中断模式下将变量的内容写入文本/XML 文件?
The scenario is that I have a long process running in debug and I have realised too late that I haven't logged enough detail about the events that the process has been monitoring, but fortunately a history is still available within a variable in the code.
这种情况是我有一个很长的进程在调试中运行,我意识到我没有记录有关该进程一直监视的事件的足够详细信息,但为时已晚,但幸运的是,历史记录在代码中的变量中仍然可用。
I could trawl through the tens of thousands of items in this list, but it's not going to persist once I hit stop on the application ... there is no obvious context option for this, but is there any way, a better way than manual? Or is there no hope and I just need to hit stop, re-tool the logging function and run the thing again?
我可以浏览此列表中的数万个项目,但是一旦我在应用程序上点击停止,它就不会持续存在……对此没有明显的上下文选项,但是有没有比手动更好的方法? 或者没有希望,我只需要点击停止,重新设置日志记录功能并再次运行它?
Aside from trying to hit a breakpoint, modify the code and re-write to make a better logger, is there a way of not losing that in-memory data?
除了尝试打断点、修改代码并重新编写以制作更好的记录器之外,有没有办法不丢失内存中的数据?
回答by JamPickle
One way to do it would be to use the immediate window (menu Debug-> Windows-> Immediate). In the window that appears you can use the "?" to query the value of a variable.
一种方法是使用即时窗口(菜单Debug-> Windows-> Immediate)。在出现的窗口中,您可以使用“?” 查询变量的值。
Assuming your history variable is a string you view its contents by typing the following in the immediate window:
假设您的历史变量是一个字符串,您可以通过在即时窗口中键入以下内容来查看其内容:
?history
You could copy and paste the output from there into a text file or alternatively ask Visual Studio to log all command window output. To do this type:
您可以将那里的输出复制并粘贴到文本文件中,或者让 Visual Studio 记录所有命令窗口输出。要执行此类型:
>log "c:\test.log"
?history
>log off
Logis an alias for Tools.LogCommandWindowOutputand accepts the following parameters:
Log是一个别名Tools.LogCommandWindowOutput并接受以下参数:
Tools.LogCommandWindowOutput [filename] [/on|/off] [/overwrite]
Check out the MSDN article Log Command Window Output Commandfor more information.
查看 MSDN 文章日志命令窗口输出命令了解更多信息。
回答by Richard Lucas
I think that my answer is pretty much the same as JamesPickrell's, but from the Immediate Window you could also do something like this:
我认为我的答案与 JamesPickrell 的答案几乎相同,但是从“立即窗口”您也可以执行以下操作:
My.Computer.FileSystem.WriteAllText("c:\temp.txt",history,True)
This would output the content of the "history" variable to a file called c:\temp.txt.
这会将“history”变量的内容输出到名为 c:\temp.txt 的文件中。
回答by angularsen
Thanks to Richard's answer, this is working for me.
感谢理查德的回答,这对我有用。
System.IO.File.WriteAllBytes(@"c:\Temp\temp.txt", myVar);
System.IO.File.WriteAllBytes(@"c:\Temp\temp.txt", myVar);
Make sure that C:\Tempexists.
确保C:\Temp存在。
The reason for writing to a folder and not to the root C:\is to avoid UnauthorizedAccessExceptionwhen not running Visual Studio as administrator.
写入文件夹而不是根目录的原因C:\是为了避免UnauthorizedAccessException在不以管理员身份运行 Visual Studio 时。
回答by Corio
I found useful/demonstrative/shareable to save the variable as a json string to the file. From Immediate Window enter the following:
我发现将变量作为 json 字符串保存到文件中很有用/示范/可共享。从立即窗口输入以下内容:
string jsonedVariable = Newtonsoft.Json.JsonConvert.SerializeObject(VARIABLE);
System.IO.File.WriteAllText(@"C:\FILENAME.json", jsonedVariable);

