We can use filter attributes to mark any action method or controller. If the attribute marks a controller, the filter applies to all action methods in that controller.
Typically, we create an action or response filter by creating an attribute class that inherits from the abstract ActionFilterAttribute class.
Some built-in action filters, such as AuthorizeAttribute and HandleErrorAttribute, inherit from the FilterAttribute class.
Other action filters, such as OutputCacheAttribute, inherit from the abstract ActionFilterAttribute class, which enables the action filter to run either before or after the action method runs.
The following tutorial shows how to create a simple action filter that logs trace messages before and after an action method is called.
So Lets Create an ASP.NET MVC internet application project
- Add a class TraceFilterAttribute that inherit from ActionFilterAttribute
- Override OnActionExecuting and OnActionExecuted methods as follow
3. Finally apply TraceFilterAttribute to your controller action on wich we want to enable trace
Run application, so you will see that each request is tracking during action and after action. And we can retrieve action name, parameters, and entire route description.
The only thing to do now is log user activity on file or database Table
4. Now Lets log runtime errors :
Our TraceFilterAttribute must implement OnException of IExceptionFilter
Each exception can now be logged.
Thank you for feedbacks
Related tutorials :
- How to configure Custom Membership Provider using ASP.NET MVC4 with external login like facebook, yahoo , google or other relying party accounts.