software development

Magic logging with interception (StructureMap, log4net and DynamicProxy)

A technical post for a change! For a while I’ve been intrigued at the possibilities that things like DI and AOP provide. I finally managed to implement one of my nebulous ideas: that of automatic logging. I achieved this with a simple StructureMap interceptor, and a Castle.DynamixProxy Interceptor. My solution is cobbled together from various pieces, most notable Ayende’s post, and this StructureMap Google Group conversation.

Herewith the code:

public class LogTypeInterceptor : StructureMap.Interceptors.TypeInterceptor
{
private readonly ProxyGenerator proxyGenerator = new ProxyGenerator();

public bool MatchesType(Type type)
{
if (type.IsSealed)
return false;

if (!type.Namespace.Contains("<root-namespace>"))
return false;

if (type.Namespace.Contains("Infrastructure"))
return false;

return true;
}

public object Process(object target, IContext context)
{
return proxyGenerator.CreateInterfaceProxyWithTargetInterface(
target.GetType().GetInterfaces().First(),
target.GetType().GetInterfaces(),
target, new LogInterceptor());
}
}

The LogInterceptor class referenced above is lifted verbatim from Ayende’s post (his class is named LoggingInterceptor).

The last piece of the puzzle is to register the Interceptor with StructureMap:


RegisterInterceptor(new LogTypeInterceptor());

Now, all types registered with the container will be ‘wrapped’ with the LogInterceptor (by being proxied). A log statement will be emitted each time any interface or virtual method is called on any registered type.

Advertisements

2 thoughts on “Magic logging with interception (StructureMap, log4net and DynamicProxy)

  1. Hi,

    Great post 🙂

    I am having an issue with the “RegisterInterceptor(new LogTypeInterceptor());” part as it doesn’t resolve. I am a bit of a newbie to this and am not sure as to what I am missing?

    Thanks
    Pierre

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s