How to register refactoring in C #?

In my services, all public methods have:

try
{
    // the method core is written here
}
catch(Exception ex)
{
    Log.Append(ex);
}

It is boring and ugly to repeat it again and again. Is there any way to avoid this? Is there a better way to keep the service running even if there are exceptions and continue to send the exception information to the class Log?

+5
source share
9 answers

You can set up a common error handling method for all excepted exceptions, for example:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);

, , , , ... , , , . , , .

+4

AOP. .

. , SO.

+5

- . :

public TResult ExecuteAndLogOnError(Func<TResult> func)
{
    try
    {
        return func();
    }
    catch(Exception ex)
    {
       // logging ...
    }
}

:

return ExecuteAndLogOnError(() =>
{
    // method core goes here..
});

4 .

+4
+3

, , , ... . , , try..catch. (IE, , , ), , ...

+2

- - , . , - :

public void Execute()
{
    try
    {
        ExecuteImplementation();
    }
    catch (Exception ex)
    {
        // Log ex
    }
}

public abstract void ExecuteImplementation();

- . ExecuteImplementation.

- :

[WebMethod]
public Result WebOperation(Request request)
{
    WebOperationClass instance = new WebOperationClass(request);
    instance.Execute();
    return instance.Result;
}
+1

. ,.NET MSIL, ++/CLI, VB.NET, #.

+1

, catch, , , ELMAH .

+1

AOP (Aspecte-Oriented Programming).

PostSharp / .

.

.

http://www.sharpcrafters.com/postsharp

- shit is no longer open source ... in any case, you can grab Postsharp1.5 and chat with it to see if you are interested.

I am in no way affiliated with PostSharp. I am just a user.

0
source

All Articles