6

I want to implement custom LogAttribute, which will log information at the start and end of each method in a class, also it should log error as well. I would like to apply the attribute over a Class.

I have seen PostSharp. But I would like to have my own custom log attribute, rather than using PostSharp or similar library.

Performance is the major key, the LogAttribute should not affect performance.

I have read about the IMessageSync, but it affect performance.

In MVC, we get CustomFilters, wherein we can have LogActionHandler, which will execute at the start and end of each Action method. I would like to implement similar in ASP.Net web Form.

Can anyone help me by providing some sample code.

svick
  • 236,525
  • 50
  • 385
  • 514
user1233802
  • 573
  • 1
  • 5
  • 21
  • 4
    Do not reinvent the wheels and use well-tested, verified by a time and community libraries – sll Mar 17 '12 at 12:10
  • Is ther any free library available for Log Attribute? PostSharp is paid..... – user1233802 Mar 17 '12 at 12:14
  • 2
    @user1233802: PostSharp has a free "Started Edition" that will allow you to implement your own custom `LogAttribute` however you'd like, and it won't affect performance since it's all done at compile time. – Allon Guralnek Mar 17 '12 at 12:16
  • 1
    Or take a look at Castle DynamicProxy http://stackoverflow.com/questions/2592787/what-really-interceptors-do-with-my-c-sharp-class – Rich Mar 17 '12 at 12:20
  • C# doesn't support AOP. So you have two solutions, either modify the generated IL (like PostSharp does), or dynamically create proxy objects (like DynamicProxy does). If you don't want to use libraries that already exist for exactly this purpose, you will need to write your own. – svick Mar 17 '12 at 13:47
  • 2
    @user1233802 As others have said, don't reinvent the wheel and if performance is key, don't go for the dynamic solutions, so in the end it is going to be PostSharp, it is on NuGet these days even, getting started takes you less then 20min and you got yourself a working and tested AOP solution for logging etc. can only recommend it, don't be scared by the need to obtain a **FREE** license – ntziolis Mar 17 '12 at 13:53
  • @AllonGuralnek: One point to note is that the Starter Edition does not include the post-build optimizer. Therefore all join points will be included in the output, even if not used. This has some performance impact - whether it is significant is another discussion, but it is good to know. – carlpett Sep 05 '12 at 22:20

1 Answers1

2

Unity - Microsoft's IoC / Dependency Injection Container also allows Method Interception.

Here's an example of a "DiagnosticsInterceptor", that does the same as you want: http://hmadrigal.wordpress.com/2010/12/25/aspect-oriented-programming-and-interceptor-design-pattern-with-unity-2/

Pro: It's free

Con: It requires Unity / DI / IoC pattern. And your class Needs to have an Interface, or a virtual method.

Unfortunately that is NOT true for ASP.NET controls. So this method will only work for your custom class: See Intercept Unity 2.0 HandlerAttribute without an interface

Community
  • 1
  • 1
Max
  • 3,260
  • 2
  • 19
  • 24