How to make EF log sql queries globally?

前端 未结 2 1031
忘了有多久
忘了有多久 2021-01-29 04:16

How do I \"tell\" EF to log queries globally? I was reading this blog post: EF logging which tells in general how to log sql queries. But I still have

相关标签:
2条回答
  • 2021-01-29 04:31

    I don't recommend to use that's functionality, because, it hasn't reason to exists in the real case. Thats it use a lot of to debug code only. But, wether you wanna know more than details ... access link... https://cmatskas.com/logging-and-tracing-with-entity-framework-6/ In this case you can put code like this

      public void Mylog()
        {
            //Thats a delegate where you can set this property to log using 
            //delegate type Action, see the code below
    
            context.Database.Log = k=>Console.Write("Any query SQL")
            //Or
            context.Database.Log = k=>Test("Any query SQL")
    
        }
    
        public void Test(string x){
    
            Console.Write(x)
        }
    

    I hope thats useufull

    0 讨论(0)
  • 2021-01-29 04:39
    1. Whenever you want the context to start logging.
    2. It appears to be done on the context object so it should be done every time you create a new context. You could add this line of code in your constructor though to ensure that it is always enabled.
    3. It will not log if you do not enable the logging.
    0 讨论(0)
提交回复
热议问题