ASP.NET MVC Tracking Run Time Errors

We can use filter attributes to mark any action method or controller. If 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 run time erros on only one function  and apply scenario to entire controllers.

So Lets Create an ASP.NET MVC internet application project

  1. Add a class TraceFilterAttribute that inherit from ActionFilterAttribute
  2. Override OnActionExecuting and OnActionExecuted methods as follow

1

3. Finally apply  TraceFilterAttribute to your controller action on wich we want to enable trace

2

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

3

4. Now Lets log runtime errors :

Our TraceFilterAttribute must implement OnException of IExceptionFilter

5

Now each exception of all controllers will be catched and logged.

Thank you for feedbacks

Related tutorials :

[contact-form][contact-field label=’Nom’ type=’name’ required=’1’/][contact-field label=’Email’ type=’email’ required=’1’/][contact-field label=’Website’ type=’url’/][contact-field label=’Comment’ type=’textarea’ required=’1’/][/contact-form]

Gora LEYE

I'm a microsoft most valuable professional (MVP) .NET Architect and Technical Expert skills located in Paris (FRANCE). The purpose of this blog is mainly to post general .NET tips and tricks, www.masterconduite.com Gora LEYE

Support us

BMC logoBuy me a coffee