How to make EF log sql queries globally?

前端 未结 2 1029
忘了有多久
忘了有多久 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

提交回复
热议问题