title: Audit trail with Entity Framework Core
body: <p>I have an ASP.NET core 2.0 using Entity Framework core on a SQL Server db.</p>
  
  <p>I have to trace and audit all the stuff made by the users on the data. My goal is to have an automatic mechanism writing all what is happening.</p>
  
  <p>For example, if I have the table Animals, I want a parallele table "Audit_animals" where you can find all the info about the data, the operation type (add, delete, edit) and the user who made this.</p>
  
  <p>I already made this time ago in Django + MySQL, but now the environment is different. I found <a href="https://github.com/thepirat000/Audit.NET/tree/master/src/Audit.EntityFramework" rel="nofollow noreferrer">this</a> and it seems interesting, but I'd like to know if there are better ways and which is the best approach to do this in EF Core.</p>
  
  <p>UPDATE</p>
  
  <p>I'm trying <a href="https://github.com/thepirat000/Audit.NET/tree/master/src/Audit.EntityFramework" rel="nofollow noreferrer">this</a> and something happens, but I have some problems.</p>
  
  <p>I added this:</p>
  
  <ol>
  <li><pre><code>services.AddMvc().AddJsonOptions(options =&gt; {
  
              options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
          }); 
  </code></pre></li>
  <li><pre><code>public Mydb_Context(DbContextOptions&lt;isMultiPayOnLine_Context&gt; options) : base(options)
  {
      Audit.EntityFramework.Configuration.Setup()
          .ForContext&lt;Mydb_Context&gt;(config =&gt; config
              .IncludeEntityObjects()
              .AuditEventType("Mydb_Context:Mydb"))
          .UseOptOut()
  }
  </code></pre></li>
  <li><pre><code>public MyRepository(Mydb_Context context)
  {
      _context = context;
      _context.AddAuditCustomField("UserName", "pippo");
  
  }
  </code></pre></li>
  </ol>
  
  <p>I also created a table to insert the audits (only one to test this tool), but the only thing I got is what you see in the image. A list of json files with the data I created.... why??</p>
  
  <p><a href="https://i.stack.imgur.com/9eQ7e.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9eQ7e.png" alt="enter image description here"></a></p>