title: What is the best way to transparently log changes to objects when using LINQ-to-SQL?
body: <p>I keep track of all <strong>changes</strong> that are made to objects so that the user can <strong>view</strong> and <strong>rollback</strong> to any previous version of any item in the database. </p>
  
  <p>The <strong>history</strong> database table looks like this:</p>
  
  <pre><code>Item     | ItemId | Field     | WhenChanged         | OldValue | NewValue
  customer | 6      | LastName  | 2009-12-31 13:00:04 | Sanders  | Sanders-Smith
  customer | 5      | FirstName | 2009-12-31 12:11:14 | Jym      | Jim
  </code></pre>
  
  <p>Currently I record these changes whenever the user fills out a <strong>form</strong> so I have complete information of the old and new state of the object.</p>
  
  <p>However, now I need to make this historical data logging available from <strong>code</strong>. It needs to work <strong>transparently</strong> when <strong>LINQ-to-SQL</strong> is used, i.e. the developer should not have to do any extra work, i.e. the following code should cause a write to the history table as well:</p>
  
  <pre><code>using (var db = Datasource.GetContext())
  {
      var customers = from c in db.Customers
          where c.Status == "waiting"
          select c;
      foreach(var customer in customers)
      {
          customer.Status = "finished";
      }
  }
  db.SubmitChanges();
  </code></pre>
  
  <p>I can imagine I can accomplish this in <strong>two</strong> ways:</p>
  
  <ol>
  <li><strong>override db.SubmitChanges()</strong> but then the question is how do I get access to the objects which are awaiting changes.</li>
  <li>attach my logging method to an <strong>OnSubmitChanges</strong> event but I haven't been able to find a solution to this yet</li>
  </ol>
  
  <p><strong>Has anyone ever worked on this problem or know of a good approach to solve it?</strong></p>