How do I disable interceptors when using them in Entity Framework

c# entity-framework entity-framework-6 web-config webdeploy


I am using Entity Framework 6.1.3 and have just implemented an interceptor so that I can view the generated SQL when developing locally. What is the correct method for disabling this interceptor when I publish my site to the LIVE environment? I am currently using a Web Config Transform through Web Deploy when publishing and I just want to make sure that the site is not trying to write to a file that doesn't exist.

I started from this article:

Here is my config section:

    <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
        <parameter value="C:\Temp\LogOutput.txt"/>
        <parameter value="true" type="System.Boolean"/>
5/3/2015 2:46:04 AM

Accepted Answer

For your purpose you do not need to write an interceptor. You can do the following setting in c# code:

    context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

After this when you are debugging all generated sql will be listed on the output. You can easily modify it to list it into a file also.

6/9/2017 5:29:07 PM

Popular Answer

To solve your scenario I would do what you are doing already and use configuration file transforms to make sure that section does not end up in the live environment.

Another option is to start using interceptors and settings them in code by using the DBInterceptors class. Then I would use C# directives to make sure the code is not called when in DEBUG mode. More on DBInterceptors here Logging and Intercepting Database Operations

I would still be using the first option though.

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