views:

41

answers:

1

Hello,

I am using the built-in forms authentication that comes with asp.net mvc. I added all the necessary tables to Sql Server using aspnet_regsql wizard and I have it all integrated and working perfect. Now I can add users, log in, perform control Authorization and all these things.

The next step is to add some basic logs when a user performs an action. For example, I want to log who has deleted an user from the database using the UserAdministration, or when someone has visited a view with restricted information (these are just examples).

I've seen that with the authentication comes a table called aspnetWebEventEvents but seems that it is intended only to store exceptions occurred on the website and it is configured with web.config and it's all performed automatically. I would like just to call a class to log custom actions without having to fill manually all the fields:

Logger.Log("Action performed");

And store automatically the user, time and all these fields.

I'm sure there has to be an already implemented class to do this stuff and I don't want to rewrite something that it is already done.

I tried using SqlWebEventProvider, that seems to be the one that is called internally but this class seems to be internal so it is not intended to be used.

Maybe I'm wrong, this is not intended to log custom actions and I should better create my own table with custom actions...

A: 

I dont know if you can accomplish that with aspnetWebEventEvents, it sound like you want to log specific situations which i don't think is the intention of this buld in feature.

Have you tried ActionFilters?, you can do really specific logging with this, for example you can check the Result Type to see what happened when the user try to use that action, and you have access to the RouteData Values through the ActionExecutedContext:

public class LogActionFilter : ActionFilterAttribute 
    {
        public override void  OnActionExecuted(ActionExecutedContext filterContext)
        {

            if (filterContext.Exception != null)
            {
                //Log - Exception Cases
            }
            else if(filterContext.Result.GetType() == typeof(RedirectToRouteResult))
            {
                //Log - Redirect to other Action
            }
           else 
            {
                //Log the normal behavior 
            }
        }

   }

And then you just Decorate the actions you want to log with this attribute:

[LogActionFilter]
public ActionResult Edit(int id)
{
}
Omar
I finally did it like this but I needed to create a custom table to log the actions.
despart