To get overview of Policy Injection Application Block (PIAB) pls read following post.
In this tutorial i will cover the steps required to use/integrate PIAB into an application. For sample i will be integrating PIAB into a console application.
Let's consider a following sample application :
using System;
using System.Collections.Generic;
using System.Text;
namespace PIAB
{
class Program
{
static void Main(string[] args)
{
TargetClass tc = new TargetClass();
tc.SayHello();
}
}
class TargetClass
{
public void SayHello()
{
Console.WriteLine("Hello from Target Class");
}
}
}
Now suppose in this we want to track the number of method calls on instances of TargetClass instances. The PIAB helps you in doing all this without writing the tracking code in methods. Let us enable the above code for PIAB integration. Our objective is to log the method calls on TargetClass in event logs using Logging Application Block.
- Add references to following assemblies :
- Microsoft.Practices.EnterpriseLibrary.Common
- Microsoft.Practices.EnterpriseLibrary.PolicyInjection
- Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers
- Microsoft.Practices.ObjectBuilder
- Microsoft.Practices.EnterpriseLibrary.Common
- Import the following namespaces :
- Microsoft.Practices.EnterpriseLibrary.PolicyInjection
- Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers
- Microsoft.Practices.EnterpriseLibrary.PolicyInjection
- Derive TargetClass from MarshalByRefObject
- Use the Create method of PIAB to create new instance of TargetClass instead of using new.
- The modified code will look like following :
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers;
namespace PIAB
{
class Program
{
static void Main(string[] args)
{
TargetClass tc = PolicyInjection.Create<TargetClass>();
tc.SayHello();
}
}
class TargetClass : MarshalByRefObject
{
public void SayHello()
{
Console.WriteLine("Hello from Target Class");
}
}
}
- Add the application config file to the application if its not added already.
- Open the app config file using Enterprise Library Configuration Tool.
- Add PIAB block to the app config file.
- In PIAB section add a new policy and say name it 'SamplePolicy'.
- In the Matching Rules section add a 'Type Matching Rule'. As we are trying to track method calls on a type and hence this rule.
- Set the value of property Matches to full type name which is 'PIAB.TargetClass'.
- We are through with the rule setting. Now we need to create handler which should get executed whenever the rule executes to true.
- Add a new 'Logging Handler' in the Handlers section.
- Set the various values of parameters in Logging Handler section as following:
- AfterMessage - Ends
- BeforeMessage - Starts
- LogBehaviour - BeforeAndAfter
- AfterMessage - Ends
- As we are using logging handler, you will have to add Logging application block also to the config file. The default options will log message in event viewer.
- After setup the PIAB section of app.config file will look like following :
<policyInjection>
<policies>
<add name="SamplePolicy">
<matchingRules>
<add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Type Matching Rule">
<matches>
<add match="PIAB.TargetClass" ignoreCase="true" />
</matches>
</add>
</matchingRules>
<handlers>
<add logBehavior="BeforeAndAfter" beforeMessage="Starts"
afterMessage="Ends" eventId="0" includeParameterValues="true"
includeCallStack="false" includeCallTime="true" priority="-1"
severity="Information" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Logging Handler" />
</handlers>
</add>
</policies>
</policyInjection>
- Build and execute the application.
- You will notice two messages in Application event logs, One with 'Starts' message and other with 'Ends' message.
- If you call the 'SayHello' method again, there will be another two messages.
PIAB is a highly customizable block and any number of Matching rules and handlers can be created.
Other Posts
- Caching App Block : Usage notes on Backing store
- Enterprise Library 3.0 : Released
- Tutorial - Using Validation Application Block
- Tutorial : Enterprise Library - Using Exception Handling Application Block
- Tutorial : Enterprise Library - Using Caching Application Block
- Tutorial : Enterprise Library - Using Logging Application Block
hi Vikas
ReplyDeleteHow do you inject the policy after the method is run?
Good article for novice.
ReplyDeleteThank You,
ANjum Rizwi
This worked excellenct, the only thing I'm missing is, how to put the name of the method called into the logger.
ReplyDeleteFor this example: How can I put the method name "SayHello" into the log file, so that I'm able to identify which method has been called?
Starting the app causes ConfigurationErrorsException. I think it fails when trying to configure the app.config.
ReplyDelete"File or assembly Microsoft.Practices.EnterpriseLibrary.PolicyInjection or one of its dependencies not found[...]".
Its referring to this section in the app.config.
...
Any Ideas?