How to implement an update LOG with ASP.NET MVC?

Asked

Viewed 761 times

5

I would like ideas on how to implement a generic update LOG in DB (Entity Framework 6+) that allows me to discover information such as: System X user at date Y changed name and date of birth (from, to) in customer registration.

I think it is possible to implement something like this in a generic way, without having to create log entries for each action in each controller, using Actionfilter and/or Entityframework Interceptor.

  • Well there’s this link: http://msdn.microsoft.com/pt-br/library/dn630210.aspx would be the basis of your question !!!

1 answer

6

Step 1. Create a Model to register the shares

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. Register your ActionFilter in the Controller desired

[CustomActionFilter]
public class MeuController : Controller
{
    ...
}

Or decorate the Actions individually

[CustomActionFilter]
public ActionResult Index()
{
  ...
}

[CustomActionFilter]
public ActionResult Search(string term)
{
  ...
}
  • 1

    It’s a good idea to record in comics?

  • And I could take the parameters passed by post?

  • You can save wherever you want. The example is to log in database, which is easier. To get the parameters by POST, use filterContext.HttpContext.Request.Form.

  • You would have some example of how to use filterContext.HttpContext.Request.Form?

  • 1

    foreach (var campo in filterContext.HttpContext.Request.Form.ToList()) { ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.