AOP With castle–Part 3-The first interceptor

Previous Parts of the series

Part 1 – The basic of interception
Part 2 – Selecting Methods to intercept

Usually the very first interceptor you can build with an AOP framework is the “logger Interceptor”, because it is simple and useful, especially when you expose some services with WPF. Consider this scenario: you expose some services with WPF, sometimes people tell you that your services have bugs or they encountered an exception, or they get wrong result, etc. In this situation you receive information like:

“I got exception from your service”, and no more details, so you need to spent time trying to understand what is happened.

Since this is probably the most detailed information you can have from the customer J, having a good log system is vital. You need to identify what is happened and you need to build a system to log:

  • every call to service methods
  • all parameters value for the call
  • return value and exception that are raised (if one).

The goal is having a full and configurable log of what is happened in the service to retrace exception and problems experienced by users. Now wait and avoid the temptation to modify every service class in the system adding log calls since you can use AOP to add logging capability to each intercepted service. Such an interceptor is really simple to build, and with few lines of code you can log every call made to a function of intercepted class.

As a requisite, you need to instantiate wcf service classes with CastleIntegration facility and Logging integration facility; thanks to these two facilities you are able to resolve the service class with castle (thus adding interceptor), and use the log4net integration to avoid hardcoding logging to specific target, like file etc. The good point in log4Net castle integration is the ability to declare a dependency to a generic ILogger interface. This feature can be used in the Logging Interceptor.

   1: public class LogAspect : IInterceptor

   2: {

   3: public LogAspect(ILogger logger)

   4: {

   5:     Logger = logger;

   6: }


   8: public ILogger Logger { get; set; }

Castle log4net facility is able to resolve the ILogger dependency with an implementation of log4net logger that has a name equal to the name of the class that declares dependency. This is the most important point, because in the above code, the logger will have the name MusicStore.Aspects.Log.LogAspect, and this permits me to change the log setting for each single class in the system. The interceptor will need also a little helper function to create a string that dump every details of the call.

   1: public static String CreateInvocationLogString(IInvocation invocation)

   2: {

   3:     StringBuilder sb = new StringBuilder(100);

   4:     sb.AppendFormat("Called: {0}.{1}(", invocation.TargetType.Name, invocation.Method.Name);

   5:     foreach (object argument in invocation.Arguments)

   6:     {

   7:         String argumentDescription = argument == null ? "null" : DumpObject(argument);

   8:         sb.Append(argumentDescription).Append(",");

   9:     }

  10:     if (invocation.Arguments.Count() > 0) sb.Length--;

  11:     sb.Append(")");

  12:     return sb.ToString();

  13: }

Since parameters of the service could be complex objects I’m dumping information with an helper function that is able to include every detail of a class.

   1: private static string DumpObject(object argument)

   2: {

   3:     Type objtype = argument.GetType();

   4:     if (objtype == typeof(String) || objtype.IsPrimitive || !objtype.IsClass)

   5:         return objtype.ToString();

   6:     return DataContractSerialize(argument, objtype);

   7: }

I want to keep the code simple, if the object type is not primitive I use DataContractSerialize to dump the content in XML format. Once everything is in place I need to insert the call to the logger in the appropriate point.

   1: public void Intercept(IInvocation invocation)

   2: {

   3:     if (Logger.IsDebugEnabled) Logger.Debug(CreateInvocationLogString(invocation));

   4:     try

   5:     {

   6:         invocation.Proceed();

   7:     }

   8:     catch (Exception ex)

   9:     {

  10:         if (Logger.IsErrorEnabled)  Logger.Error(CreateInvocationLogString(invocation), ex);

  11:         throw;

  12:     }

  13: }

Before each call to the logger object I first check if the appropriate level of logging is enabled. This technique is useful to avoid loss of performance when log is not enabled; if the debug level is set to warn, the Logger.Debug will not log anything and the CreateInvocationLogString will build the log string for nothing, losing processor time with no benefit. To avoid this loss you can issue a call to Logger.IsDebugEnabled to avoid entirely the call to logging function.

Now suppose that the caller pass an invalid object to method Save() of MusicStoreService, the user will see a message telling that a service error is occurred, but now I’m able to check the log to understand exactly what is happened. Here is the log of a call that raise an exception.

   1: 2010-07-24 10:12:28,320 ERROR MusicStore.Aspects.Log.LogAspect - Called: MusicStoreService.Save(<Album xmlns:i="" xmlns="">

   2:   <Id>0</Id>

   3:   <Author

   4:     i:nil="true" />

   5:   <Genre

   6:     i:nil="true" />

   7:   <Image

   8:     i:nil="true" />

   9:   <Label

  10:     i:nil="true" />

  11:   <Note

  12:     i:nil="true" />

  13:   <PublicationDate>0001-01-01T00:00:00</PublicationDate>

  14:   <Title

  15:     i:nil="true" />

  16:   <Tracks

  17:     i:nil="true" />

  18: </Album>)

  19: NHibernate.PropertyValueException: not-null property references a null or transient valueMusicStore.Entities.Album.Title

  20:    at NHibernate.Engine.Nullability.CheckNullability(Object[] values, IEntityPersister persister, Boolean isUpdate)

  21:    at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)

  22:    at NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)

  23:    at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)

From this log I understand that an invalid object is passed to the service, the property Album.Title is required in the database, but the user passed a property with null value. Since log4net is really flexible I’m able to dump this information to a file, to a database, to network or with mail. You can as example send a mail each time an exception occurs, so you are immediately notified if something in the service is not going well.

This logger can be improved a little bit because the name of the logger is always MusicStore.Aspects.Log.LogAspect for each wrapped service. This is not really a problem, but I prefer to have the ability to configure logging differently for each service; in real product with a lot of services, this is a key requiremente. The interceptor can be changed in this way:

   1: public class LogAspect : IInterceptor

   2: {

   3:     public LogAspect(ILoggerFactory loggerFactory)

   4:     {

   5:         LoggerFactory = loggerFactory;

   6:         Loggers = new Dictionary<Type, ILogger>();

   7:     }


   9:     public ILoggerFactory LoggerFactory { get; set; }


  11:     public Dictionary<Type, ILogger> Loggers { get; set; }

Now the interceptor declares a dependency to an ILoggerFactory and not to a concrete ILogger, and caches a list of ILogger object based on type. The result is a concrete ILogger object for each wrapped type.

   1: public void Intercept(IInvocation invocation)

   2: {

   3:     if (!Loggers.ContainsKey(invocation.TargetType))

   4:     {

   5:         Loggers.Add(invocation.TargetType, LoggerFactory.Create(invocation.TargetType));

   6:     }

   7:     ILogger logger = Loggers[invocation.TargetType];

   8:     if (logger.IsDebugEnabled) logger.Debug(CreateInvocationLogString(invocation));

instead of using the same logger, we first check if we had already created a logger for a given type, if false we use the ILoggerFactory to create the logger and cache it to an inner dictionary. If we send an invalid object again to the service the head of the log is.

   1: 2010-07-24 10:27:30,783 DEBUG MusicStore.WebService.MusicStoreService - Called: MusicStoreService.Save(..

Now the name of the logger is equal to the name of the concrete service class and you have created a simple logging system that can:

  1. Add transparently to each service class without needing a single line of code
  2. Change logging level for each concrete class of the service.

Happy intercepting with Castle :)


Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

Leave a Reply

Your email address will not be published.