Is it possible to get parameters' values for each frame in call stack in .NET
Asked Answered
D

2

13

I'm talking about managed .NET code. If we run any program and attach VS to it we can see parameters' values for each method in call stack. I'd like to create a logging solution which will log all parameters' values for each method in call stack. Actually I need this info in case an exception occurs.

I know it's possible with profiling API. But I wonder is it possible with only managed code?

UPDATE: Ok, probably with pure .NET it's impossible. Then may be with some kind of unmanaged code... the point is to do this from within application itself. An application in case of an exception could call some library (may be unmanaged) which returns info about methods' values in call stack. Just thoughts...

Dictaphone answered 4/5, 2009 at 10:16 Comment(3)
Note that optimizations, especially in "Release" mode, inlining etc., can make information not to show up in the call stack. You should better not rely on this kind of information.Recessional
Sure, app's logic shouldn't rely on such info. But I'm just talking about logging for diagnostic purpose.Dictaphone
I understood that. Still, if you get a log extract and don't have reliable information in it, its usefulness is only limited.Recessional
D
7

Parameters values aren't stored in StackFrame instance. In fact, they aren't recorded/logged at all, unless you choose to do it explicitly.

One obvious way to log theses values is to use AOP. It would certainly imply a cost, but in conjunction with a logging framework and the right log level, it may be an alternative. You could also choose to instrument only some types/methods in your basecode, where exceptions are more likely to be thrown. I would probably choose Postsharp for its static weaving capabilities, to emit log calls.

Anyway, it's far from being an ideal solution, but I'm afraid you won't have many options if you're stuck in the managed world.

Dinnerware answered 4/5, 2009 at 10:40 Comment(1)
This answer doesn't provide any help. AOP is very vast subject and this doesn't point what to look for?Hercegovina
G
2

Your best option is probably to insert the required trace code in the relevant methods. That way you can attach trace listeners and dump values when needed.

I know it is not what you're asking for, but it is one way to get the data.

Alternatively, you can debug the application using WinDbg. The !clstack/!dso commands will let you inspect parameters and stack objects.

Gantline answered 4/5, 2009 at 10:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.