Aspect Oriented Programming(AOP) with PostSharp in C# .NET - Example

Manuj
Posted by in C# category on for Intermediate level | Points: 250 | Views : 3536 red flag

The article explains how to use PostSharp to do logging in existing code.

Introduction

The article explains how to use PostSharp for doing logging in the existing code.

Using the code

Suppose in your C# .NET application you have a requirement that you want to log the Entry and Exit of every method. One of the ways to do this is by writing log statements at every start and end of the method but that will be a performance nightmare.


Here comes the magic of AOP!


 AOP is something that increases the modularity by allowing separation of concern which in this case is Logging.Now you could read about Aspect Oriented Programming over the net but that is not the topic of today.


There are a number of frameworks that are based on AOP like Castle, Encase, Spring .NET amd one of them is PostSharp.


Today we will see how to do Entry/Exit level logging at every method using PostSharp.


You could download free edition of PostSharp from here

http://www.postsharp.net/download


Now add a reference of PostSharp.dll to your C# project. Create a attribute something like below


[Serializable]

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]

    [MulticastAttributeUsage(MulticastTargets.Method, AllowMultiple = true)]

    public class LoggingAttribute : OnMethodBoundaryAspect

    {

         public override void OnEntry(MethodExecutionArgs args)

        {

           Console.WriteLine("On Method Enter");

        }


        public override void OnExit(MethodExecutionArgs args)

        {

           Console.WriteLine("On Method Exit");

        }

    }


Suppose you have a method Test and you want to log the Entry/Exit of this method.You can decorate the method with the attribute [Logging] as below and it would automatically print the messages on Console.

[Logging]
public void Test()
{
}


You could also use OnException method in case of Unhandled Exception in the method.


Page copy protected against web site content infringement by Copyscape

About the Author

Manuj
Full Name: Manuj Sardana
Member Level: Starter
Member Status: Member
Member Since: 4/21/2014 3:16:32 PM
Country: India

http://www.dotnetfunda.com/manuj
Software Developer with more than 8 years of experience in developing application based on C# .NET, ASP.NET, WPF, Windows Forms, Silverlight

Login to vote for this post.

Comments or Responses

Posted by: Hongdida on: 6/24/2014 | Points: 25
Posted by: Allensmith on: 1/15/2015 | Points: 25
Thanks for your sharing,i learn a lot.I have quite a large code base using a variety of different ADO technologies (i.e. some EF and in some cases using ADO.Net directly).I'm wondering if there is any way to globally intercept any ADO.Net calls so that I can start auditing information - exact SQL statements that executed, time taken, results returned, etc.The main idea being that if I can do this, I shouldn't have to change any of my existing code and that I should be able to just intercept/wrap the ADO.Net calls...Is this possible?
print itf in .net:
http://www.keepautomation.com/products/net_barcode/barcodes/itf_14.html

Login to post response

Comment using Facebook(Author doesn't get notification)