Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing your own runtime analysis of your code in C#

I have written a large C# app with many methods in many classes.

I'm trying to keep a log of what gets called and how often during my development. (I keep a record in a DB)

Every method is padded with the following calls:

void myMethod()
{
log(entering,args[]);

log(exiting,args[]);
}

Since I want to do this for all my methods, is there a better way to do this then having to replicate those lines of code in every method?

like image 463
Matt Avatar asked Mar 01 '23 10:03

Matt


2 Answers

Take a look at http://www.postsharp.org/

Here's the first example on their front page:

public class TraceAttribute : OnMethodBoundaryAspect 
{ 
  public override void OnEntry( MethodExecutionEventArgs eventArgs) 
  { Trace.TraceInformation("Entering {0}.", eventArgs.Method);  } 

  public override void OnExit( MethodExecutionEventArgs eventArgs) 
  { Trace.TraceInformation("Leaving {0}.", eventArgs.Method);   } 
}

Seems to be exactly what you need!

like image 164
Blorgbeard Avatar answered Apr 28 '23 06:04

Blorgbeard


Well, you could use some kind of Decorator on your classes to log things, but I think the better approach is to use some kind of profiling tool in your build and/or unit testing process. You can pretty easily get a lot of value out of some simple metric collection in CruiseControl, etc.

like image 28
Peter Loron Avatar answered Apr 28 '23 08:04

Peter Loron