Yugz
Yugz

Reputation: 677

Whats the best way to log method calls?

i need to log each method call in my application. i dont want to repeat code in each method of the business layer

i have been looking at the AOP approach which seems to be the best option right now but i have no idea how to use this method.

any suggestions or links articles or examples would be appreciated.

Upvotes: 1

Views: 718

Answers (1)

eyossi
eyossi

Reputation: 4330

You can use PostSharp which is an APO infrastructure and you have a nice example on that link for ways of using it.

If you need any logging infrastructure too you can use log4net.

From the links above, you can see the log attribute example that you can just modify for using a logging infrastructure as well:

[Serializable]
public class LogAspect : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        Console.WriteLine(Environment.NewLine);

        Console.WriteLine("Entering [ {0} ] ...", args.Method);

        base.OnEntry(args);
    }

    public override void OnExit(MethodExecutionArgs args)
    {
        Console.WriteLine("Leaving [ {0} ] ...", args.Method);

        base.OnExit(args);
    }
}

using it on your code:

[LogAspect]
public void SomeMethod()
{
}

which will log on SomeMethod call

Upvotes: 1

Related Questions