Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've done this by creating a wrapper class around the ObjectContext and using that wrapper instead of the original context. Here's an example context wrapper:</p> <pre><code>public partial class LoggedContext : MyContext { public LoggedContext() : this("name=MyEntities") // Adjust this to match your entities { } public LoggedContext(string connectionString) : base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(connectionString) { } private EFTracingConnection TracingConnection { get { return this.UnwrapConnection&lt;EFTracingConnection&gt;(); } } public event EventHandler&lt;CommandExecutionEventArgs&gt; CommandExecuting { add { this.TracingConnection.CommandExecuting += value; } remove { this.TracingConnection.CommandExecuting -= value; } } public event EventHandler&lt;CommandExecutionEventArgs&gt; CommandFinished { add { this.TracingConnection.CommandFinished += value; } remove { this.TracingConnection.CommandFinished -= value; } } public event EventHandler&lt;CommandExecutionEventArgs&gt; CommandFailed { add { this.TracingConnection.CommandFailed += value; } remove { this.TracingConnection.CommandFailed -= value; } } } </code></pre> <p>I also have a static class that defines the tracing output method and has a static method to initialize tracing. Here:</p> <pre><code>public static class EFTracingExtensions { private static ILogger _logger; public static void InitSqlTracing(ILogger logger) { _logger = logger; EFTracingProviderConfiguration.RegisterProvider(); if (logger.IsLoggingEnabled()) // Don't add logging hooks if logging isn't enabled { EFTracingProviderConfiguration.LogAction = new Action&lt;CommandExecutionEventArgs&gt;(AppendSqlLog); } } private static void AppendSqlLog(CommandExecutionEventArgs e) { if (e.Status != CommandExecutionStatus.Executing) // we only care about Finished and Failed { StringBuilder msg = new StringBuilder(e.ToTraceString().TrimEnd()); msg.Append(Environment.NewLine); if (e.Result is SqlDataReader) { int rows = ((SqlDataReader)e.Result).HasRows ? ((SqlDataReader)e.Result).RecordsAffected : 0; msg.AppendFormat("*** {0} rows affected", rows); } else if (e.Result is int) { msg.AppendFormat("*** result: {0}", e.Result); } else { msg.AppendFormat("*** finished, result: {0}", e.Result); } msg.Append(Environment.NewLine); msg.AppendFormat(" [{0}] [{1}] in {2} seconds", e.Method, e.Status, e.Duration); _logger.Log(msg.ToString(), LoggerCategories.SQL); } } } </code></pre> <p>ILogger is the logging interface I'm using. You need to substitute your own interface/methods.</p> <p>The InitSqlTracing method is invoked once when my program starts up, and then the LoggedContext class is used to log all the SQL generated by Entity Framework.</p> <p>Putting it all together with your sample code:</p> <pre><code>EFTracingExtensions.InitSqlTracing(logger); // only call this once using (var context = new LoggedContext()) { var a = from data in context.Projects select data; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload