Created
January 4, 2013 21:47
-
-
Save akimboyko/4457261 to your computer and use it in GitHub Desktop.
AOP: handwritten tracing decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
TraceDecorator.Aspect(() => StaticLogic.SuccessfulCall()); | |
TraceDecorator.Aspect(() => StaticLogic.ExceptionCall()); | |
TraceDecorator.Aspect(() => StaticLogic.SuccessfulCallWithReturn(42)).Dump(); | |
TraceDecorator.Aspect(() => StaticLogic.ExceptionCallWithReturn(42)).Dump(); | |
} | |
public static class TraceDecorator | |
{ | |
public static T Aspect<T>(Func<T> func) | |
{ | |
try | |
{ | |
return func(); | |
} | |
catch(Exception ex) | |
{ | |
LogException(ex); | |
return default(T); | |
} | |
} | |
public static void Aspect(Action func) | |
{ | |
try | |
{ | |
func(); | |
} | |
catch(Exception ex) | |
{ | |
LogException(ex); | |
} | |
} | |
private static void LogException(Exception ex) | |
{ | |
Console.WriteLine("Traced by TraceDecorator: {0}", ex); | |
} | |
} | |
public static class StaticLogic | |
{ | |
public static void SuccessfulCall() | |
{ | |
} | |
public static void ExceptionCall() | |
{ | |
throw new Exception(); | |
} | |
public static string SuccessfulCallWithReturn(int input) | |
{ | |
return string.Format("ok: input {0}", input); | |
} | |
public static string ExceptionCallWithReturn(int input) | |
{ | |
throw new Exception(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment