如何跟踪 C# 中代码的执行情况
如何跟踪 C# 应用程序的代码执行?有没有可用的工具?我的生产站点出现问题。
how can I trace code execution of my C# application? Are there any tools available? I have an issue in my production site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Visual Studio 中有一个内置工具,称为调试器。
您在代码中设置断点并单步执行。
.NET 框架还在 跟踪 类中提供 < href="http://msdn.microsoft.com/en-us/library/15t15zda.aspx" rel="noreferrer">System.Diagnostics 命名空间。
对于不支持跟踪的代码的正在运行的应用程序,您可以使用探查器(例如 redgate ANTZ 探查器或 JetBrains dotTrace),但这会影响性能。
如果您有内存转储(来自崩溃或 手动诱导的),可以使用windbg 分析转储。这将包括跟踪信息。
There is a built in tool in visual studio called a debugger.
You set a breakpoint in your code and step through.
The .NET framework also provides tracing classes in the System.Diagnostics namespace.
For a running application that has no code support for tracing, you may be able to use a profiler (such as the redgate ANTZ profiler or the JetBrains dotTrace), but this will impact performance.
If you have a memory dump (either from a crash or an manually induced one), you can use windbg to analyse the dump. This will include trace information.
还有 JetBrains dotTrace 也很棒。
There is also JetBrains dotTrace which is excellent.
您能否在代码中将调试信息(例如“现在处于方法 1”)写入日志文件?
您可能会找到诸如 log4net 和 PostSharp 有帮助。
Can you, in your code, write the debug information ( e.g, "now at method 1") to a log file?
You might find tools like log4net and PostSharp helpful.
您可以使用一些外部日志库(例如 Log4Net),甚至使用 Visual Studio 的执行远程调试和单步执行代码的能力。
You could use some external log library (such as Log4Net), or even use Visual Studio's ability to perform remote-debugging and step inside your code.
在 Visual Studio 2005+ 中,按 F11 而不是 F5 来运行应用程序。
或者您可以插入一个断点(单击要中断的行的左侧,以便 VS 显示一个红点)。
In Visual Studio 2005+ press F11 instead of F5 to run the application.
Or you can insert a breakpoint (clicking to the left of the line where you want to break so that VS shows a red dot).
Visual Studio 有很棒的调试工具,但是如果这还不够,那么您可以从静态代码分析工具开始,这有助于分析代码分支。
Visual studio has great debugging tool however if that too terned out to be not sufficient then you can start with static code analysis tools which are helpful to analyze the code branches.
要跟踪生产问题,最好的选择是 ETW 跟踪。特别是在 ASP.NET 中,有内置的提供程序可以帮助识别性能问题。
如果您使用的是 Window2008,那么 ETW 跟踪可以提供调用堆栈。有来自 IIS、文件系统、注册表、线程和一切可能的 ETW 跟踪。这是一篇关于此问题并获取托管调用堆栈的 MSDN 文章我有一些帖子
To trace production Issues the best option is ETW tracing. Especially in ASP.NET there are built-in providers which can help identify the Perf Issues.
And if you are using Window2008 then the ETW traces can give call-stacks. There are ETW tracing from IIS, FileSystem , Registry , Threading and everything possible. Here is an MSDN article on this and to get Managed call-stacks I have few posts