0

I have a web api, I generate all models with Entity Framework with a database-first approach.

Then what I do is I create all controllers with actions using Entity Framework , what I want is to find a way to audit all changes by all crud actions (create, read, update, delete) done in those controllers. This way of auditing those changes should be done automatically for me, what I mean by this is that if I add a new controller created with entity framework with all actions, it should have the auditing thing.

For example, if someone does a get of /api/Example/4, what I want is now, to have a tuple in my database telling the id of the user that did the action, the type of operation (in this case a get), etc..

This is my Audit class created with EF:

 [Table("Audit")]
public partial class Audit
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal AuditId { get; set; }

    public DateTime AuditDate { get; set; }

    [Required]
    [StringLength(100)]
    public string AuditTableName { get; set; }

    [Required]
    public string AuditDescription { get; set; }

    [Required]
    [StringLength(400)]
    public string AuditShortDescription { get; set; }

    [Required]
    [StringLength(3)]
    public string AuditAction { get; set; }

    public short UserId { get; set; }

    public virtual Usuario Usuario { get; set; }
}
  • You could write a custom logger to do this. Check https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx – Fortega Jan 24 '18 at 12:47
  • Try out [Audit.NET](https://github.com/thepirat000/Audit.NET) library, you can configure automatic auditing at the [EF context level](https://github.com/thepirat000/Audit.NET/blob/master/src/Audit.EntityFramework/README.md) and/or at [WebAPI level](https://github.com/thepirat000/Audit.NET/blob/master/src/Audit.WebApi/README.md). – thepirat000 Jan 26 '18 at 23:31
  • I finally could resolve it. I had to create a CustomHandler and add it to the Register method in the web api config. Then the code i wrote it manually. Thank you anyways :) – Pablito0951 Jan 31 '18 at 16:41

0 Answers0