2828using Common . Logging ;
2929
3030using Patterns . Collections . Strategies ;
31+ using Patterns . ExceptionHandling ;
32+ using Patterns . Interception ;
3133
3234namespace Patterns . Logging
3335{
@@ -36,7 +38,7 @@ namespace Patterns.Logging
3638 /// uses <see cref="ILog" /> to log Trace, Debug, Info, and Error
3739 /// events throughout the execution of intercepted invocations.
3840 /// </summary>
39- public class LoggingInterceptor : IInterceptor
41+ public class LoggingInterceptor : DelegateInterceptor
4042 {
4143 private const string _nullArgument = "[NULL]" ;
4244 private const string _argumentListFormat = "({0})" ;
@@ -53,54 +55,74 @@ private static readonly FuncStrategies<Type, object, object> _displayStrategies
5355
5456 private static readonly JavaScriptSerializer _jsonSerializer = new JavaScriptSerializer ( ) ;
5557
58+ /// <summary>
59+ /// Initializes a new instance of the <see cref="LoggingInterceptor"/> class.
60+ /// </summary>
61+ /// <param name="config">The config.</param>
62+ /// <param name="logFactory">The log factory.</param>
63+ public LoggingInterceptor ( ILoggingConfig config , Func < Type , ILog > logFactory ) : this ( config , logFactory , null ) { }
64+
5665 /// <summary>
5766 /// Initializes a new instance of the <see cref="LoggingInterceptor" /> class.
5867 /// </summary>
5968 /// <param name="config">The config.</param>
6069 /// <param name="logFactory">The log factory.</param>
61- public LoggingInterceptor ( ILoggingConfig config , Func < Type , ILog > logFactory )
70+ /// <param name="condition">The intercept condition.</param>
71+ public LoggingInterceptor ( ILoggingConfig config , Func < Type , ILog > logFactory , Func < IInvocation , bool > condition )
6272 {
63- _config = config ;
6473 _logFactory = logFactory ;
74+ _config = config ;
75+ Initialize ( condition ) ;
6576 }
6677
67- /// <summary>
68- /// Intercepts the specified invocation.
69- /// </summary>
70- /// <param name="invocation">The invocation.</param>
71- public void Intercept ( IInvocation invocation )
78+ private void Initialize ( Func < IInvocation , bool > condition )
79+ {
80+ Condition = condition ;
81+ Before = LogBefore ;
82+ After = LogAfter ;
83+ OnError = LogError ;
84+ Finally = LogFinally ;
85+ }
86+
87+ protected virtual void LogBefore ( IInvocation invocation )
7288 {
7389 ILog log = _logFactory ( invocation . TargetType ) ;
7490 log . Trace ( handler => handler ( LoggingResources . MethodStartTraceFormat , invocation . TargetType , invocation . Method . Name ) ) ;
7591 log . Debug ( handler => handler ( LoggingResources . MethodArgsDebugFormat , invocation . Method . Name , GetMethodArguments ( invocation ) ) ) ;
92+ }
7693
77- try
78- {
79- invocation . Proceed ( ) ;
80- log . Info ( handler => handler ( LoggingResources . MethodInfoFormat , invocation . Method . Name , LoggingResources . MethodInfoPass ) ) ;
81- }
82- catch ( Exception error )
83- {
84- log . Info ( handler => handler ( LoggingResources . MethodInfoFormat , invocation . Method . Name , LoggingResources . MethodInfoFail ) ) ;
85- log . Error ( handler => handler ( LoggingResources . ExceptionErrorFormat , invocation . Method . Name , error . ToFullString ( ) ) ) ;
86- if ( ! _config . TrapExceptions ) throw ;
87- }
88-
94+ protected virtual void LogAfter ( IInvocation invocation )
95+ {
96+ ILog log = _logFactory ( invocation . TargetType ) ;
97+ log . Info ( handler => handler ( LoggingResources . MethodInfoFormat , invocation . Method . Name , LoggingResources . MethodInfoPass ) ) ;
8998 log . Trace ( handler => handler ( LoggingResources . MethodStopTraceFormat , invocation . TargetType , invocation . Method . Name ) ) ;
99+ }
90100
101+ protected virtual ExceptionState LogError ( IInvocation invocation , Exception error )
102+ {
103+ ILog log = _logFactory ( invocation . TargetType ) ;
104+ log . Info ( handler => handler ( LoggingResources . MethodInfoFormat , invocation . Method . Name , LoggingResources . MethodInfoFail ) ) ;
105+ log . Error ( handler => handler ( LoggingResources . ExceptionErrorFormat , invocation . Method . Name , error . ToFullString ( ) ) ) ;
106+ return new ExceptionState ( error , _config . TrapExceptions ) ;
107+ }
108+
109+ protected virtual void LogFinally ( IInvocation invocation )
110+ {
91111 if ( invocation . ReturnValue == null ) return ;
92112
113+ ILog log = _logFactory ( invocation . TargetType ) ;
114+
93115 object value = ConvertValueForDisplay ( invocation . ReturnValue ) ;
94116 log . Debug ( handler => handler ( LoggingResources . MethodReturnDebugFormat , invocation . Method . Name , value ) ) ;
95117 }
96118
97- private static string GetMethodArguments ( IInvocation invocation )
119+ protected static string GetMethodArguments ( IInvocation invocation )
98120 {
99121 object [ ] arguments = invocation . Arguments . Select ( ConvertValueForDisplay ) . ToArray ( ) ;
100122 return string . Format ( _argumentListFormat , string . Join ( _argumentListSeparator , arguments ) ) ;
101123 }
102124
103- private static object ConvertValueForDisplay ( object value )
125+ protected static object ConvertValueForDisplay ( object value )
104126 {
105127 if ( value == null ) return _nullArgument ;
106128
0 commit comments