title: C# Entity Framework Logging (DbContext) not working
body: <p>I'm trying to log the sql that is being sent to the database by Entity framework, but not having a tremendous amount of luck. I'm working the code first to new database workflow, but as I'm adding properties to my DbContext I get an Oracle error back from the database - identifier too long. I'd like to look at the sql that is being sent to the database to identify how it is trying to create my tables and see which identifier name is too long.</p>
  
  <p>Currently I have the following in place, but it doesn't ever appear to be called while the code is running. I know data is being sent to the database because I get an exception, but the logger never gets executed. There's something really weird going on here. It's almost as if 99% of the time, the interceptors don't work.</p>
  
  <pre><code>public class DBLog
  {
      private string path = @"C:\users\myuser\desktop\oracleloggingtest1.txt";
  
      public void Log(string message)
      {
          File.AppendAllText(path, message);
      }
  }
  
  ...
  ...
  
  DBLog lggr = new DBLog();
  Database.SetInitializer(new DropCreateDatabaseAlways&lt;TestDbContext&gt;());
  using (var context = new TestClearPersonModel())
          {
              context.Database.Log = s =&gt; lggr.Log(s);
  
              context.WorkAffiliations.Add(personReportResult.WorkAffiliations);
  
  
              ...
  
              context.SaveChanges();
          }
  </code></pre>
  
  <p>An exception from entityframework is thrown on the context.workaffiliations.add line from Oracle because one of the identifiers is too long, but none of this info is logged.</p>
  
  <p>At some point the other day when I was messing around with this, the logging worked because I do have a log file from a couple days ago on my desktop with some old data in it. I just can't figure out why it is not currently working. I've tried a few different methods now to get this data.</p>
  
  <p>Any help is appreciated!</p>
  
  <p>EDIT:
  Some interesting developments trying Tipx suggestion... I've been trying all night to be able to log the sql commands that are going to the database using different methods. I had the method above, I had an IDbCommandInterceptor setup registered programmatically by a function call before the db operations begin, and then I have this in my app config in the entityframework section:</p>
  
  <pre><code>&lt;interceptors&gt;
    &lt;interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"&gt;
      &lt;parameters&gt;
        &lt;parameter value="C:\users\myuser\desktop\orclogtst.txt" /&gt;
      &lt;/parameters&gt;
    &lt;/interceptor&gt;
  &lt;/interceptors&gt;
  </code></pre>
  
  <p>None of this code has been running. I had breakpoints on every different logging mechanism I've tried and they never got called. I've tried dozens and dozens of times with little tweaks here and there. After I made a small change recommended by Tipx (just trying to log to console), suddenly both of the other logging mechanisms worked. I hit the breakpoint in my IDbCommandInterceptor which logged data, and it turns out that the interceptor from the web.config file also logged data to the file specified there....</p>
  
  <p>But this only worked on that execution. I immediately tried to rerun my code (without changing anything) and it no longer worked. I undid the changes to the code, compiled, then added the changes recommended by Tipx again, and it still didn't work. What could cause these methods to suddenly get called without any code changes and not get called again since?</p>