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?
Log
You can set up a common error handling method for all excepted exceptions, for example:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledException);
, , , , ... , , , . , , .
AOP. .
. , SO.
- . :
public TResult ExecuteAndLogOnError(Func<TResult> func) { try { return func(); } catch(Exception ex) { // logging ... } }
:
return ExecuteAndLogOnError(() => { // method core goes here.. });
4 .
. WCF . : http://www.haveyougotwoods.com/archive/2009/06/24/creating-a-global-error-handler-in-wcf.aspx
, IServiceBehavior, . , - , ( , , , ) .
, , , ... . , , try..catch. (IE, , , ), , ...
- - , . , - :
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; }
. ,.NET MSIL, ++/CLI, VB.NET, #.
, catch, , , ELMAH .
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.