C# 如何转储所有变量 &运行时的当前值

发布于 2024-08-06 22:55:31 字数 180 浏览 5 评论 0原文

是否有任何内置或第三方库允许您在运行时简单地将所有变量转储到内存中?我想要的是能够查看变量&当前值类似于通过击中断点并将鼠标悬停在变量上来查看它们,但实际上不必停止程序执行(即仅获取快照)。如果它可以将它们转储到一个文件中,然后可以稍后在程序中打开该文件以获得一个漂亮的 GUI 界面来查看它们,那就太好了,但简单的文本文件转储就足够了。

Are there any in-built or 3rd party libraries that allow you to simply dump all variables in memory during run time? What I would like is to be able to view variables & current values similarly to viewing them by hitting a break point and hovering over variables, but without actually having to halt the program execution (i.e. just get a snapshot). Would be good if it could dump them to a file which can then be opened later in a program to get a nice GUI interface to view them, but simple text file dump would be good enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

氛圍 2024-08-13 22:55:31

我想不出一种简单的方法来以通用的方式做到这一点。可行的方法是以编程方式创建正在运行的进程的转储文件。您可以使用 P/Invoke 执行此操作 dbghelp.dll 例程或生成 cdb.exe 进程来创建转储文件。获得该文件后,您可以在调试器中打开它,以便稍后使用 SOS 进行分析.dll 与 cdb.exe/windbg.exe,甚至编写一个 调试器脚本 来转储(大部分)自动获取您想要的数据。

I can't think of an easy way to do this in a generic fashion. What could work is programmatically creating a dump file of your running process. You could either do this with P/Invoke to the dbghelp.dll routines or spawn a cdb.exe process to create the dump file. Once you have the file, you could open it up in a debugger for later analysis using SOS.dll with cdb.exe/windbg.exe, or even write a debugger script to dump the data you want (mostly) automatically.

原来分手还会想你 2024-08-13 22:55:31

我相信某种日志记录框架可以帮助您做到这一点...

查看:

http://www.dotnetlogging。 com/

在我的工作场所,我们使用 log4net 这对我们来说效果很好。

那么为什么你想转储所有变量以供以后分析呢?您是否考虑过首先编写代码测试,以便减少对调试器的依赖,并有一套自动测试为您检查值?

I believe some sort of logging framework would help you to do that...

Check out:

http://www.dotnetlogging.com/

At my workplace we use log4net which works pretty well for us.

So how come you're wanting to dump out all the variables for later analysis? Have you considered writing your code test first so that you can reduce your reliance on the debugger and have a suite of automated test checking the values for you?

浪荡不羁 2024-08-13 22:55:31

过去,我使用 YourKit .Net 分析器来分析 .Net 应用程序。

虽然我只用它来连接到个人正在运行的应用程序,但快照文档 确实声明他们有一个 Profiler API 可用于以编程方式转储快照以供以后查看。

从代码角度来看,这看起来很简单,如下所示:

Controller c = new Controller();
String snapshotPath = c.CaptureSnapshot();

我相信您可以稍后将快照文件加载到 YourKit GUI 中以进行查看。

如果其他一些流行的分析器如 JetBrains dotTrace Performance 和 RedGates ANTS Performance Profiler 有类似的编程 API,但我无法快速找到他们的网站上有明显的文档(我不想观看他们的网络研讨会来了解此功能是否存在!)

In the past I've used the YourKit .Net profiler in order to profile .Net applications.

While I've only ever used it to connect to running applications personally the Snapshot documentation does state that they have a Profiler API that can be used to programmatically dump snapshots for later review.

Code wise this looks to be as simple as the following:

Controller c = new Controller();
String snapshotPath = c.CaptureSnapshot();

I believe you can then load the snapshot files into the YourKit GUI at a later date to review them.

I would not be surprised if some of the other popular profilers like JetBrains dotTrace Performance and RedGates ANTS Performance Profiler have similar programmatic APIs but I couldn't quickly find obvious documentation on their websites (and I didn't want to watch their webinars to find out if this feature existed!)

高跟鞋的旋律 2024-08-13 22:55:31

为此,您可以使用 WMemoryProfiler

  • 所有应用程序域中的所有对象作为对象数组获取
  • 创建您自己的内存转储将
  • 特定对象序列化到光盘

要实现这一点,您当然需要 Windbg,但 WMemoryProfiler 的 Api 是完全托管的,您基本上可以自我调试您的进程。该库解决了常见的调试器奇怪问题,因为它确实将 Windbg 包装在一个很好的可访问库中。

下面的代码确实将 System.Threading.Thread 对象的所有实例获取到对象数组中。这样您就可以在运行时为您自己的应用程序对象编写可视化工具。另一个重载只是简单地为您提供所有 AppDomain 中的所有对象。

using (var debugger = new MdbEng())
{
  var dummy = new Thread(() => {});
  dummy.Name = "Dummy Thread";

  // Get all thread objects in all AppDomains
  var threads = debugger.GetObjects("System.Threading.Thread", true);

  foreach (Thread t in threads)
  {
    Console.WriteLine("Managed thread {0} has Name {1}", t.ManagedThreadId, t.Name);
  }
  GC.KeepAlive(dummy);
}

由于它是 Windbg 的包装器,您还可以动态创建内存转储,然后从进程中加载​​内存转储,以从转储中提取对象数据以进行可视化。商业内存分析器(例如 Scitech 的 MemoryProfiler)多年来一直采用这种技术,但当您有大量内存转储时,速度会相当慢,因为它们也使用 Windbg 作为转储分析器。

For this you can use WMemoryProfiler to

  • Get all objects in all appdomains as an object array
  • Create a memory dump of your own process
  • Serialize specific objects to disc

To make this happen you need Windbg of course but the Api of WMemoryProfiler is fully managed and you can basically self debug your process. The library takes care of the usual debugger oddities since it does wrap Windbg in a nice accessible library.

The code below does get all instances of System.Threading.Thread objects into an object array. This way you can write a visualizer for your own application objects at runtime. The other overload does simply give you all objects in all AppDomains.

using (var debugger = new MdbEng())
{
  var dummy = new Thread(() => {});
  dummy.Name = "Dummy Thread";

  // Get all thread objects in all AppDomains
  var threads = debugger.GetObjects("System.Threading.Thread", true);

  foreach (Thread t in threads)
  {
    Console.WriteLine("Managed thread {0} has Name {1}", t.ManagedThreadId, t.Name);
  }
  GC.KeepAlive(dummy);
}

Since it is a wrapper around Windbg you can also create a memory dump on the fly and later load a memory dump from your process to extract object data for visualization from the dump. Commerical Memory Profilers (e.g. MemoryProfiler from Scitech) employ this technique since years but it is quite slow when you have a huge memory dump since they are using also Windbg as dump analyzer.

日记撕了你也走了 2024-08-13 22:55:31

您可以尝试使用 Visual Studio 终极版提供的 Intellitrace 工具。这就是您所描述的 - 它记录您的应用程序中发生的情况,并允许您在不执行程序的情况下调试它,并将鼠标悬停在变量和所有其他调试窗口上来帮助您。

You can try Intellitrace tool provided with ultimate version of visual studio. It is what you describe - it records what is happening in your app and allows you to debug it without executing your program with hovering over variables and all other debug windows to help you.

夏尔 2024-08-13 22:55:31

您可以使用 PostSharp 。我发现记录调试时间非常有用,因为环境应用程序已部署。并记录/记录了很多东西。

但显然您需要指定需要记录的所有变量。

请在此处查看更多详细信息。

You can use PostSharp . I found it very useful to record debug times because of the environment application was deployed. And instrumented/recorded many things.

But obviously you'll need to specify all variables you need to record.

Check more details here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文