dimanche 2 octobre 2016

Better way to add logging or a method that needs to occur in every function?

Is there a better way to log or add a method calls to a series of functions without explicitly adding in the function calls?

For example I have a class like:

public class MyClass
{

public void DoStuff()
{
doSomethingEnter();

//MyCode

doSomethingExit();
}

public void DoStuff_2()
{
doSomethingEnter();

//MyCode

doSomethingExit();
}
}

doSomethingEnter() and doSomethingExit() can be logging calls or events calling other things that need to occur on function start and end.

I have read on Aspect Oriented Programming that allows me to do something like this:

public class MyClass
{

[DoSomethingEnterExit]
public void DoStuff()
{
   //MyCode
}
}

But is there a pattern or framework that allows me to do something similar without having to buy an AOP framework like PostSharp?

Thanks.

Aucun commentaire:

Enregistrer un commentaire