Step 1. Create a Model
to register actions
namespace SeuProjeto.Models
{
public class ActionsLog
{
[Key]
public Guid ActionsLogId { get; set; }
public String Controller { get; set; }
public String Action { get; set; }
public String Ip { get; set; }
public DateTime DateAndTime { get; set; }
// Coloque aqui os campos extras que deseja usar para o registro
}
}
Step 2. Implement a Filter derived from ActionFilter
namespace SeuProjeto.Filters
{
public class CustomActionFilter : ActionFilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
// Coloque aqui algumas regras de negócio que quiser antes de fazer o log.
// Aqui é o registro propriamente dito
var contexto = new MeuProjetoContext();
var log = new ActionsLog()
{
ActionsLogId = Guid.NewGuid(),
Controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
Action = filterContext.ActionDescriptor.ActionName + " (Logged By: Custom Action Filter)",
Ip = filterContext.HttpContext.Request.UserHostAddress,
DateAndTime = filterContext.HttpContext.Timestamp
// Coloque aqui os campos extras para inclusão
};
contexto.ActionsLogs.Add(log);
contexto.SaveChanges();
this.OnActionExecuting(filterContext);
}
}
}
Step 3. Record your ActionFilter
in the desired% w /%
[CustomActionFilter]
public class MeuController : Controller
{
...
}
Or decorate the Controller
individually
[CustomActionFilter]
public ActionResult Index()
{
...
}
[CustomActionFilter]
public ActionResult Search(string term)
{
...
}