开发者

How to get a code or stacktrace?

I am trying to figure out the execution path for some code (i.e. which methods it hits and in what order). Is there a tool, or means through visual studio or the web.config to show the order of fun开发者_高级运维ctions that a program is going through?

Right now I am placing debug points in certain areas and hoping it hits the right area, but in some cases this is a long slow tedious process.


If you're trying to figure out what code is calling a particular method, you could try System.Diagnostics.StackFrame:

var s = new System.Diagnostics.StackFrame();
Console.WriteLine(s.ToString());

StackFrame will give you the stack trace for the current thread.
See MSDN: http://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe.aspx

If you're looking for more detail than that then a profiler may be a better option for you. I've used dotTrace from JetBrains a bit and it does provide a lot of detail.

EDIT:
ASP.NET also provides tracing functionality. If you add the following to your web.config:

<system.web>
    <trace enabled="true" />
    ...
</system.web>

and then add Trace="true" to your page directive:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Trace="true" %>

then you'll get a bunch of trace output appended to the bottom of your page when you view it.

Hope that helps.


Some .NET profiling tools can provide this kind of analysis (often called Call Graph Analysis). One that I am aware of is the ANTS profiler.

However, if you're looking for a detailed trace of every method that is executed and what parameters were passed - you may find that hard to get with any tool. If that's what you actually need, you may need to use a tool like PostSHARP to weave in trace logic on each method call (which you can do with the MethodBoundaryAspect).


Tutorials:

  • Rich Custom Error Handling with ASP.NET
  • Exception Handling Advice for ASP.NET Web Applications

Handlers & Libraries

  • log4net
  • elmah


Well, one method I always use is to add either printfor OutputDebugString in the functions (suppose you have the source code). You can make these debug information only show up in debug version by using some conditional definitions.


Runtime Flow (developed by me) shows the order of functions that a .NET program is going through.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜