Welcome to weblogs.com.pk Sign in | Join | Help

AOP :: Logging Using Spring Framework - Part 2

Now that we have our auto-magic logging system configured; we want that the invoked method/code can log certain things; and that information is only known by the invoked code. Spring Framework setup its context and it can be used to get reference to the weaved aspects; but its not recommended; as our code then has a direct dependence on the AOP framework; which is against the spirit of AOP.

Fortunately; .NET runtime has an Application Domain feature that can be exploited; the AppDomain is setup by .NET runtime and we can get and set values in the app domain. The invoked code can store the data that needs to be logged into the App Domain which then weaved aspect can retrieve and consume. Here is the proof of concept.

The invoked code

public System.Data.DataSet GetIssuedItemsSummary(string issuedBy)
    if ( !this.parseParameter(issuedBy) ) return null;
string sql = this.getIssuedItemsSql();
AppDomain.CurrentDomain.SetData("sql", sql);
Database db = DatabaseFactory.CreateDatabase();
DbCommand cmd = db.GetSqlStringCommand(sql);
"@IssuedBy", DbType.String, issuedBy);
return db.ExecuteDataSet(cmd);

The aspect

#region IMethodInterceptor Members
public object Invoke(IMethodInvocation invocation)
    //The same aspect code till invocation.Proceed()
    object returnValue = invocation.Proceed();
    object oData = null;
    try { oData = System.AppDomain.CurrentDomain.GetData("sql"); }
catch { }
if ( null != oData)
Logger.LogInformation(invocation.Method.Module.Name + ".txt", oData.ToString());
    return returnValue;

Published Saturday, February 4, 2006 11:43 PM by khurram
Filed under: ,


No Comments

New Comments to this post are disabled