28
28
using Common . Logging ;
29
29
30
30
using Patterns . Collections . Strategies ;
31
+ using Patterns . ExceptionHandling ;
32
+ using Patterns . Interception ;
31
33
32
34
namespace Patterns . Logging
33
35
{
@@ -36,7 +38,7 @@ namespace Patterns.Logging
36
38
/// uses <see cref="ILog" /> to log Trace, Debug, Info, and Error
37
39
/// events throughout the execution of intercepted invocations.
38
40
/// </summary>
39
- public class LoggingInterceptor : IInterceptor
41
+ public class LoggingInterceptor : DelegateInterceptor
40
42
{
41
43
private const string _nullArgument = "[NULL]" ;
42
44
private const string _argumentListFormat = "({0})" ;
@@ -53,54 +55,74 @@ private static readonly FuncStrategies<Type, object, object> _displayStrategies
53
55
54
56
private static readonly JavaScriptSerializer _jsonSerializer = new JavaScriptSerializer ( ) ;
55
57
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
+
56
65
/// <summary>
57
66
/// Initializes a new instance of the <see cref="LoggingInterceptor" /> class.
58
67
/// </summary>
59
68
/// <param name="config">The config.</param>
60
69
/// <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 )
62
72
{
63
- _config = config ;
64
73
_logFactory = logFactory ;
74
+ _config = config ;
75
+ Initialize ( condition ) ;
65
76
}
66
77
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 )
72
88
{
73
89
ILog log = _logFactory ( invocation . TargetType ) ;
74
90
log . Trace ( handler => handler ( LoggingResources . MethodStartTraceFormat , invocation . TargetType , invocation . Method . Name ) ) ;
75
91
log . Debug ( handler => handler ( LoggingResources . MethodArgsDebugFormat , invocation . Method . Name , GetMethodArguments ( invocation ) ) ) ;
92
+ }
76
93
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 ) ) ;
89
98
log . Trace ( handler => handler ( LoggingResources . MethodStopTraceFormat , invocation . TargetType , invocation . Method . Name ) ) ;
99
+ }
90
100
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
+ {
91
111
if ( invocation . ReturnValue == null ) return ;
92
112
113
+ ILog log = _logFactory ( invocation . TargetType ) ;
114
+
93
115
object value = ConvertValueForDisplay ( invocation . ReturnValue ) ;
94
116
log . Debug ( handler => handler ( LoggingResources . MethodReturnDebugFormat , invocation . Method . Name , value ) ) ;
95
117
}
96
118
97
- private static string GetMethodArguments ( IInvocation invocation )
119
+ protected static string GetMethodArguments ( IInvocation invocation )
98
120
{
99
121
object [ ] arguments = invocation . Arguments . Select ( ConvertValueForDisplay ) . ToArray ( ) ;
100
122
return string . Format ( _argumentListFormat , string . Join ( _argumentListSeparator , arguments ) ) ;
101
123
}
102
124
103
- private static object ConvertValueForDisplay ( object value )
125
+ protected static object ConvertValueForDisplay ( object value )
104
126
{
105
127
if ( value == null ) return _nullArgument ;
106
128
0 commit comments