views:

290

answers:

3

Hi, I have an application I need to analyze. I have the source code here. I already found a way to log the method calls using this code:

Inside Method: logger.MethodTraceLog();

    public void MethodTraceLog()
    {
        var msg = "MethodTraceLog: " 
            + this.log.Logger.Name 
            + " ### "
            + new StackFrame(1).GetMethod().Name ;

        this.log.Debug(msg);
    }

Is there any way to "inject" the logging call into every method of a class, without having to rewrite the whole source code. As I said, I could, but it just is a lot of work. Some "post function call via reflection" in the constructor, or anything similar?

Thanks for tips... If anybody has some links additional for analyzing the "behaviour" of an application, I would not say no to it :-) Any framework out there? (Except breakpoints and a lot of F-Key hitting)

+3  A: 

Actually, there is a concept called Aspect Oriented Programming (AOP) and an implementation in c# called PostSharp (http://www.postsharp.org/) that allows you to inject code post compilation.

Student for Life
+1 I was just googling for that. I could remember the concept of the tool but not the name :)
DeletedAccount
Thanks, looks very promising. Will try it and provide some feedback here.
Christian
There's also special plugin for Post# called Log4PostSharp, which is exacly what You are looking for I think.
matma
Thanks, worked like a charm. For anyone reading this, definitly worth the "trouble" of a 5 min install. Will save me a lot of time.
Christian
A: 

I recommend you do two things: one, get NDepend.

Two, get the Visual Studio 2010 beta 1. Run it in a VM if necessary. It will generate sequence diagrams from code, I believe, and has other features to help comprehend a code base at a high level. You don't have to use it for anything other than understanding.

The downside is that I hereby pass along to you the moral obligation to report bugs you find, on http://connect.microsoft.com/visualstudio.

:-)

John Saunders
A: 
Saul Dolgin