6

I want to log the Entity Framework queries in my debug window. I could do that with the following line:

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

But how can I do that for all my queries in different functions and in different files?

Do I have to write this line everywhere?

Or is there a way to do this by writing a particular line of code to log every query at a single place.

As suggested, I have written the code in the constructor of the context but it's not working.

public partial class EkartEntities : DbContext
{
    public EkartEntities() : base("name=EkartEntities")
    {
        Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
    }
}

Am I doing something wrong?

Also, it is not duplicate of How to make EF log sql queries globally? as the post contains the answer of Code-First approach where we can simply modify our constructor.

17
  • After writing your line of code, all queries against your database context will be logged, no matter in which function or file you use this context. You don't have to write this line of code again. Commented Sep 13, 2018 at 6:56
  • 1
    Possible duplicate of How to make EF log sql queries globally? Commented Sep 13, 2018 at 6:57
  • 1
    Yes, you're right. If you like to overwrite it globally you could simply set this property within the constructor of your context. In that case every instance will do the log. Commented Sep 13, 2018 at 7:11
  • 2
    You could write a factory method, that creates the context and sets the logger. Then you replace all your ctor calls with this factory method. Commented Sep 13, 2018 at 11:23
  • 2
    It's impossible to make a partial with a constructor when the other class already has one (which a generated context does). Either your code can't compile or the partial class is not in the same namespace (so effectively it's another, unrelated class). Commented Sep 13, 2018 at 15:45

2 Answers 2

3

You can install global logger by adding the following class to the project containing your DbContext derived class:

class MyDbConfiguration : System.Data.Entity.DbConfiguration
{
    public MyDbConfiguration()
    {
        AddInterceptor(new System.Data.Entity.Infrastructure.Interception.DatabaseLogFormatter(
            s => System.Diagnostics.Debug.WriteLine(s)));
    }
}

The class represents the so called Code-based configuration and in this particular case is used to automatically register DatabaseLogFormatter with the specified Action<string> for all DbContext derived types and instances in the project that contains it.

Sign up to request clarification or add additional context in comments.

Comments

-1

I think it is better that you can refer to use SQL Server Profiler to capture all the queries to database.

If you are developing locally, SQL Profiler will be ideal for you.

Whatever kind of Linq Queries or Raw SQL Query can be captured by the profiler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.