Examples using Attributes?

Topics: Logging Application Block, Policy Injection Application Block
Jun 6, 2007 at 12:36 PM
Hi,

I'm fairly new to the Enterprise Library and especially the Policy Injection stuff, so please excuse me if I ask weird and "noobie" questions... :)

Anyways, I'm trying to log all calls to a certain method by using the LogCallHandler attribute, but I'm unable to get anything logged. So I was wondering; What do I have to configure for this to work? Any particular Trace Listener or anything special in the Policy Injection part of the config file?

Any help would be most appreciated.

regards,
Jarle Nygård
Jun 6, 2007 at 4:07 PM
You need to do a couple things.

1) Add the Logging Application Block to your app.config or web.config and make sure it has a default category that gets logged somewhere. The LogCallHandler Attribute will only log to the default category.

2) Write some code like below that is interceptable and uses the Policy Injection Application Block:

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MyClass myClass = PolicyInjection.Create<MyClass>();
            myClass.MyMethod("test");
        }
    }
 
    public class MyClass : MarshalByRefObject
    {
        [LogCallHandler]
        public void MyMethod(string myParameterId)
        {
            
        }
    }
}

Regards,

Dave

___________________________________

David Hayden
Microsoft MVP C#
Jun 7, 2007 at 12:25 PM
Thanx a bunch! I misunderstood the part obout using the PolicyInjection.Create<MyClass>(); code. So I was basically just creating my class as I normally would. Now I'm using a factory pattern, so my team members won't have to deal with the Policy Injection stuff.

Once more: Thanx for your help.

regards,
Jarle Nygård