Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement custom LogAttribute on a Class

Tags:

c#

logging

aop

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.

like image 334
user1233802 Avatar asked Mar 17 '12 12:03

user1233802


1 Answers

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

like image 51
Max Avatar answered Oct 17 '22 13:10

Max