AOP :: Logging Using Spring Framework
Few days back I weblogged about how I used Spring Framework for .NET’s AOP features to implement logging. Here are my notes of it; and it will show you how easy things are using Aspect Oriented Programming.
Lets assume we have already implemented the following classes.
public interface IInventorFunctionality { }
internal class InventoryImplementation : IInventorFunctionality { }
internal class Logger { }
As InventoryImplementation class is internal; there exists a factory class that instantiate it and return a reference. Something like:
public static IInventorFunctionality GetInventoryFunctionality()
{
return new InventoryImplementation();
}
Now for the logging; we need to write an aspect; that will intercept all the methods and log the class, method name and the parameter values being passed. For such an aspect; we need to write a class that implement, AopAlliance.Intercept. IMethodInterceptor interface. Here is such an aspect class.
internal
class InventoryTraceAspect : IMethodInterceptor
{
public InventoryTraceAspect() {}
#region IMethodInterceptor Members
public object Invoke(IMethodInvocation invocation)
{
string data = invocation.Method.DeclaringType.UnderlyingSystemType.FullName + "::";
data += invocation.Method.Name;
data += "(";
if (null != invocation.Arguments)
{
int i = 0;
foreach (object o in invocation.Arguments)
{
i++;
if (null != o)
data += o.ToString();
else
data += "null";
if (i < invocation.Arguments.Length) data += ", ";
}
}
data += ")";
Logger.LogInformation(invocation.Method.Module.Name + ".txt", data);
object returnValue = invocation.Proceed();
return returnValue;
}
#endregion
}
As you can see, most of the code above is to create the string for the logging. Otherwise it’s a straightforward code, nothing special to mention. Last two lines need some attention; we are basically calling Proceed() method to tell the Spring framework to proceed with the method invocation; the framework will return what our method returns and we simply return it as-is. Such aspects can be used to validate inputs and to do other similar things; if you are not happy somewhere; don’t call Proceed(), raise an exception or return some invalid value to notify the caller.
Now to glue our aspect and the code we already have written; we need to modify our factory method.
public
static IInventorFunctionality GetInventoryFunctionality()
{
Spring.Aop.Framework.ProxyFactory pf = new ProxyFactory(new InventoryImplementation());
pf.AddAdvice(new InventoryTraceAspect());
return (IInventorFunctionality)pf.GetProxy();
}
Here we have added an advice and returns a proxy being generated by Spring framework dynamically which will call our aspect auto-magically. That’s it; we have added the logging support with just writing two lines (the rest of the lines we had to write anyways)
In next post; I will show you how we can append to the log file some other useful information; say the invoked method execute some query; and we need to log that query.