开发者

How to track the current line of code executed by another process in C#

I was wondering if it is possible to get the number of the line of code which is currently executed by another process (within the same program) in c#. Say we have a class "TestClass" which starts two processes: Process A and B. Process A executes a bunch of code while Process B looks from time to time at what line of code process A is. Or it could be the other way round: Process A tells process B at every finished line of code that it just executed that line.

In the end I would like to be able to log a timestamp + the current line of code being executed to a textfile.

Obviously I could have a million Console.WriteL开发者_如何学编程ine("Line x");'s in my code, but I would like my application to be more flexible with regard to code changes.

I would appreciate any hint to a library / API that is capable of doing such a thing. Code snippets are also highly appreciated. :)

Best wishes, Christian


You may want to describe your goal more fully, as your intent drives this answer.

If your goal is doing performance analysis (my guess), figuring out what parts of your system are slow, check out tools like JetBrains DotTrace. If you use any homebrew method in the way you describe, you'll modify/destroy the performance you're trying to analyze.

If your goal is a state monitor (like whether your program is in one state or another) you'll want to build some kind of system so that it will record its current state in a very controlled way, rather than just dumping every line # as you reach it (so I doubt this is your goal).

If your goal is debugging, the VS debugger is your tool of choice.

If you want to trace your system flow, then logging (like the commenter's log4net) is what you should look at.


What is the motivation behind doing this? If you just want to understand about the call graph and how long it takes to execute a particular line, wouldn't running your code on a runtime profiler be sufficient?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜