Toan Nguyen
Toan Nguyen

Reputation: 11581

How to trace all methods in an application

Do you know how to trace all method invocations in an .NET application. My current approach is to write trace at the beginning of each method like this

void DoSomething()
{
   using(new Tracer(TraceCategory)
{
   //Perform all action here

}

}

But it sucks as it clusters my code and I will have to write traces for all of my method.

Would there another way to record when a method is called, and when it finishes? All suggestions would be appreciate.

Upvotes: 1

Views: 1159

Answers (2)

AD.Net
AD.Net

Reputation: 13399

You should use frameworks like Unity or PostSharp. Both of them have hooks for before method starting and after method finishing events.

Sample for Unity

Sample for PostSharp was already posted by Yahia.

Upvotes: 3

Yahia
Yahia

Reputation: 70369

The only practical option to implement this I know of is to use AOP (like postsharp) - for a nice walkthrough see here.

Upvotes: 4

Related Questions