2

in c#, is there any practicable way to intercept a method line after line, at run-time?

the specific application of interest would be dynamic logging:

if something within a method body threw an exception then on subsequent executions every line of that method would be logged some way or another. it is not known at compile-time which specific methods will throw an exception, so the technique should be able to interfere with most methods in the source code (which could be prepared in some generic way beforehand)

in the imaginary world, the interceptor would also expose parameters for easy identification and evaluation of the specifics of the last line of code executed.

but given this may not be possible at all, is there any rough proxy available for such a tool?

grateful as always.

Cel
  • 6,467
  • 8
  • 75
  • 110
  • 1
    I'm not sure if it's what you're asking for, but the idea of Aspect Oriented Programming (AOP) lets you intercept exceptions and take actions similar to what you're describing. PostSharp is a popular .NET AOP library. – lance Sep 08 '11 at 19:41
  • 1
    How does the runtime know what a "line" is, or an AOP framework that does post processing? In IL there is no such thing as a line. – vcsjones Sep 08 '11 at 19:42
  • @lance ive played with postsharp a bit, but i cannot see a way to do it with that library? – Cel Sep 08 '11 at 19:47
  • @vcsjones no idea, would [pdb files](http://stackoverflow.com/questions/41842/pdb-files-for-production-app-and-the-optimize-code-flag) be a small part of the whole answer? – Cel Sep 08 '11 at 19:47
  • 1
    There is already such a tool, its called a debugger. – Ben Robinson Sep 08 '11 at 19:47
  • I've not used it enough to say. I have seen someone catch exceptions (system-wide) with an aspect using PostSharp, and I suppose you could remember that and take actions in future exceptions (or invocations) around that method. As for /line by line/, though: to @vcsjones' point, I'm not sure that's possible. – lance Sep 08 '11 at 19:48
  • 1
    No - there's not really a practical way to do this. The only way of really doing this is via a profiler - which is really a C++ rather than C# (managed) level API... you could do this - here's a starting place - http://msdn.microsoft.com/en-gb/magazine/cc188743.aspx - but don't expect it to be easy! – Stuart Sep 08 '11 at 19:50
  • 1
    @Ben: not true. This is for production use. You don't want to spam your logs, but if something "interesting" happens you'd want to dial up logging to capture any immediate evidence that might help deduce what happened. – Joe Sep 08 '11 at 19:51
  • @BenRobinson :) what about such a debugger for production (which does not log everything) ? – Cel Sep 08 '11 at 19:52
  • Here's a reply [from PostSharp: Line-by-line interception is not possible (although you can do pretty much everything you want with PostSharp SDK), but you can log the value of all arguments by using the OnException aspect or advise.](http://www.sharpcrafters.com/forum/Topic7440-19-1.aspx#bm7442) – Cel Sep 08 '11 at 20:23

1 Answers1

2

I believe the only thing that comes close to what you want is ELMAH. You can modify ELMAH to do stuff -- so I guess you could use it as a starting point. However, the only way to get the line by line type functionality would be to write your own debugger -- pop in the int 13s on every line as defined in the debugging info (the pdb files you mention) -- it is a lot of work.

With ELMAH you can see a whole lot about the state of the system most importantly the stack trace.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Thanks for the reference! Could you possibly point me to the [parts of ELMAH](http://code.google.com/p/elmah/) I should look into specifically, so I could better evaluate whether it could mould to my requirements? – Cel Sep 08 '11 at 20:04
  • hmmm... well here is an example ELMAH module that loads to FogBugz : http://www.billforney.com/post/2010/11/01/Combining-the-goodness-that-is-ELMAH-with-FogBugz.aspx You can find much documentation on this page http://code.google.com/p/elmah/wiki/WebBase or with a google search. – Hogan Sep 08 '11 at 21:09