Logging - AOP with Unity

Mar 8, 2010 at 9:04 PM

Hello Guys,

I am trying to do exactly what is shown in the video under Pattern and Practices Guidance. linky

Now here is the code I have so far:

public class LogAttribute : HandlerAttribute
        public override ICallHandler CreateHandler(IUnityContainer container)
            return new LoggingHandler();

public class LoggingHandler : ICallHandler
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
            // write to some logging file here.
            var result = getNext()(input, getNext);
            // do stuff after.
            return result;

        public int Order { get; set; }

Now here is what I want to do:

I want to be able to go over any method in any class and just put


(include any namespace ofcourse)

and it should log.

What would I do for this to happen? I went down the path of creating a ILogger and a Logger file that implements this as shown in the example.

public interface ILogger
        void Write(string message);

    public class Logger : ILogger
        public void Write(string message)

Doing this, I had to do the following to "register" my class and type with container:

IUnityContainer container = new UnityContainer();


            container.RegisterType<ILogger, Logger>().
                Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());

            var logger = container.Resolve<ILogger>();

            logger.Write("My test code is logging.");

While this works, but I am not able to figure out how I would do this for the 100.. classes out in my solution.

Please help.

Mar 8, 2010 at 10:21 PM

First off, pnpguidance.net is not from Microsoft or from patterns & practices. That site is run by independent people with no connection to us.

Second, and your actual question, there's nothing built in to automate the registration process. You'll need to write some code to spin through your types and configure interception for each one. The RegisterType and SetInterceptorFor methods also have overloads that take Type objects as well, so it's fairly easy to do.



Mar 9, 2010 at 2:11 AM

Thanks for your quick response and noted your comments on pnp.

Any samples that can show me how that is done?



Mar 9, 2010 at 3:59 AM

No samples from us at this time. If you're comfortable with the reflection APIs, it's basically calling Assembly.GetExportedTypes and looping through them.

I've got an assembly scanner on my personal backlog for something to write and publish after Entlib 5 / Unity 2 get out the door, but it's just me, not something on p&p's schedule, so no promises on if / when it'll happen.




Mar 9, 2010 at 11:53 AM

Something does not add up in my mind when I am thinking thro this. Here are the steps I am guessing you are indicating at (using the example above):

step 1: create two classes exactly like LogAttribute and LoggingHandler

step 2: using reflection, search all classes that implement [log] <-- that sounds interesting. I have done searching using reflection using namespace; search by attribute is new to me. 

step 3: loop thro the collection and execute a code similar to below for each found in collection:

                       container.RegisterType<ILogger, Logger>().
                Configure<Interception>().SetInterceptorFor<ILogger>(new InterfaceInterceptor());

If I understand right, this should be enough. Now if I have a method called SaveToDB() in some class somewhere and I decorate it with [Log], when this method is called ...automatically I should get into my handler.

This is what I am not getting...would this be enough or there is more to be done?




Mar 9, 2010 at 5:17 PM

That's basically it. Like I said, it's not hard. :-)