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: https://msdn.microsoft.com/en-us/data/jj556606.aspx#Interceptors
Here is my config section:
<interceptors> <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"> <parameters> <parameter value="C:\Temp\LogOutput.txt"/> <parameter value="true" type="System.Boolean"/> </parameters> </interceptor>
For your purpose you do not need to write an interceptor. You can do the following setting in c# code:
#if DEBUG context.Database.Log = s => System.Diagnostics.Debug.WriteLine(s); #endif
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.
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.