I have several methods that look like this:
public void foo()
{
try
{
doSomething();
}
catch(Exception e)
{
Log.Error(e);
}
}
Can I change the code to look like?
[LogException()]
public void foo()
{
doSomething();
}
How can I implement this custom attribute? and what are the pros and cons of doing it?
—–Edit 1————
Can I implemented it myself, I mean just write one class, or do I need to use postsharp or another solution?
You can use delegates and lambdas:
In fact, you could rename
ExecuteWithLoggingto something likeExecuteWebserviceMethodand add other commonly used stuff, such as checking credentials, opening and closing a database connection, etc.