Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trace a variable at runtime in C#?

How can I track a variable's values as they change, at runtime, in C#? I'm interested in the same functionality that the debugger provides when I'm tracing a variable through execution steps, only that I need to call upon it from my code. Some sort of key-value observing, but for all kinds of variables(local, class, static, etc), not only properties. So, basically, receive a notification when a variable's value changes.

like image 412
luvieere Avatar asked May 26 '10 10:05

luvieere


People also ask

How can you examine a variable while executing code?

Hover over a variable to see its value. The most commonly used way to look at variables is the DataTip. When stopped in the debugger hover the mouse cursor over the variable you want to look at. The DataTip will appear showing you the value of that variable.

Can a variable be changed at run time?

Answer: Explanation: Variable is a place where memory are stored or you can assume it also as a container of memory which can be used in different part of the program when needed and yes variable can change during runtime.

How do you declare a variable at runtime in C++?

In C++ programming, we can declare memory at run time for any type variable like char, int, float, integer array etc. To declare memory at run time (dynamically), we use new operator. I would recommend reading about new operator first.

What is runtime variable?

With runtime variables, you can define values that can be used as source values in Outbound Mappings or Conversation Variable Mappings in the same rule. This way the actual mappings can be made simpler, more readable and you can avoid repeating the same kind of configuration many times.


3 Answers

You are working from the assumption that the debugger can track variable changes. It can't.

It is possible with unmanaged code, the processor has dedicated debug registers that allow setting data breakpoints. Up to three are provided. It generates a hardware interrupt when it sees a particular memory location getting written. This otherwise very useful feature isn't available in managed code however. The garbage collector is completely incompatible with it, it moves objects around, giving them another address.

The managed debugger does support a "when hit" condition on a breakpoint, allowing you to dump info to the output window. That however requires a breakpoint, it cannot be triggered by a change in variable value. It also really slows down code execution since the debugger actually enters a break state before executing the condition.

The obvious place to put such a breakpoint is in a property setter. Which is what you'll need to implement this feature in code. You can do anything you want in that setter, using the Trace class for example.

like image 164
Hans Passant Avatar answered Oct 08 '22 10:10

Hans Passant


To add to what Marc said, if you want to do this for lots of properties and methods you might want to check out aspect oriented programming techniques, and libraries such as PostSharp.

http://www.sharpcrafters.com/postsharp

like image 41
RationalGeek Avatar answered Oct 08 '22 09:10

RationalGeek


The managed debugger uses the ICorDebug COM API for pretty much everything. The part that you're interested is ICorDebugValue and its descendants. Note that a LOT of the debugging API requires that the process be not running (ie, have encountered a breakpoint) in order for the various inspections to happen. A high level overview of ICorDebug is here. The documentation on it is kinda sparse, but some Googling may help. Good luck.

like image 42
Donnie Avatar answered Oct 08 '22 09:10

Donnie