C# Entity Framework Logging (DbContext) not working

c# entity-framework entity-framework-6


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.

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.

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<TestDbContext>());
using (var context = new TestClearPersonModel())
            context.Database.Log = s => lggr.Log(s);




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.

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.

Any help is appreciated!

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:

  <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
      <parameter value="C:\users\myuser\desktop\orclogtst.txt" />

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....

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?

5/2/2017 3:41:33 AM

Accepted Answer

Clearing out the database results in the logging working normally. If I try and run my code without clearing out the database first, the logging doesn't work. I suspect this is because this was happening in the middle of trying to generate my database tables from my model classes, rather than some normal crud operation.

5/3/2017 6:14:06 PM

Popular Answer

I don't think it has to do with the provider. The file might be open/locked, or the permissions might do something.

Here is what I'd do to find out what the problem is :

Remove the context.Database.Log = s => lggr.Log(s); line, and in your context constructor, add this :

Database.Log = (s) => { Console.WriteLine(s); };

And then put a breakpoint on Console.WriteLine, run the program and see if you hit it. If you do, you know it has nothing to do with the provider. If you don't, then double-check what class context.

Also, on a sidenote: you should look into using a logging framework like NLog or Log4Net. The first time around it might take you half an hour to get it working, but it's worth it. Also, I personally set the loggers in the class themselves; the class using the context shouldn't tell your context to log, it's none of it's business! :-P

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow