tags:

views:

1620

answers:

7

I am having a hard time tracking down a lock issue, so I would like to log every method call's entry and exit. I've done this before with C++ without having to add code to every method. Is this possible with C#?

+2  A: 

Use ANTS Profiler from Red Gate would be your best bet. Failing that, look into interceptors in Castle Windsor. That does assume you're loading your types via IoC though.

Reflection is another way, you can use the System.Reflection.Emit methods to "write" code into memory. That code could replace your method's code, and execute it but with appropriate logging. Good luck on that one, though... Easier would be to use an Aspect Oriented Programming framework like Aspect#.

Neil Barnwell
ANTS is great (as is the VS built in profiler), but the drawback is you might change the memory/timing 'footprint' of the app. and possibly not experience the lock...
Mitch Wheat
Tracing would also change the timing.
Mark Brackett
Yes, it's Heisenberg's Uncertainty Principle. By observing the experiment you may well change the results of the experiment. That's thread sync debugging for you...
Neil Barnwell
+8  A: 

Probably your best bet would be to use an AOP (aspect oriented programming) framework to automatically call tracing code before and after a method execution. A popular choice for AOP and .NET is PostSharp.

Dennis G.
Thanks - PostSharp even comes with an example project that does something similar. :)
Jon Tackabury
+5  A: 

Be warned, doing this will most likely change the timing of your app significantly enough that your locking issue disappears.

Paul Betts
Very true - even hooking a debugger to the application while it's running prevents the issue from occurring. I didn't end up going this route, I found the cause using WinDbg and dozens of stacktrace dumps.
Jon Tackabury
A: 

How do you know that it's happening? If this is a multithreaded application, i would recommend testing for the condition and calling System.Diagnostics.Debugger.Break() at runtime when it's detected. Then, simply open up the Threads window and step through the call stacks on each relevant thread.

Robert Venables
+1  A: 

A profiler is great for looking at your running code during development but if you're looking for the ability to do custom traces in production, then, as Denis G. mentionned, PostSharp is the perfect tool: you don't have to change all your code and you can easily switch it on/off.

It's also easy to set-up in a few minutes and Gaël Fraiteur, the creator of PostSharp even has a video that shows you how easy it is to add tracing to an existing app.
There are also examples and tutorials in the documentation section.

Renaud Bompuis
A: 

It might be waiting for the lock issue to take hold, doing a memory dump and analysing the call stack on various threads. You can use DebugDiag or the adplus script (hang mode, in this case) that comes with Debugging Tools for Windows.

Tess Ferrandez also has an excellent lab series on learning to debug various issues using .NET memory dumps. I highly recommend it.

Richard Szalay
A: 

if you have a deadlock issue check out http://www.codeproject.com/KB/dotnet/Deadlock%5FDetection.aspx

eee