[英]How to make EF log sql queries globally?
How do I "tell" EF
to log queries globally? 如何“告诉” EF
以全局方式记录查询? I was reading this blog post: EF logging which tells in general how to log sql
queries. 我正在阅读此博客文章: EF日志记录 ,它通常介绍如何记录sql
查询。 But I still have a few questions regarding this logger. 但是关于这个记录器我仍然有一些问题。
context.Database.Log = s => logger.Log("EFApp", s);
我需要在哪里放置此行context.Database.Log = s => logger.Log("EFApp", s);
? ? In the "Failed execution" section, the blogger wrote that, and I quote: 在“执行失败”部分,博主写道,我引用:
For commands that fail by throwing an exception, the output contains the message from the exception. 对于因引发异常而失败的命令,输出将包含来自异常的消息。
Will this be logged too if I don't use the context.Database.Log
? 如果我不使用context.Database.Log
也将记录此日志吗?
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 但是,您是否想知道更多细节...访问链接... https://cmatskas.com/logging-and-tracing-with-entity-framework-6/在这种情况下,您可以放置这样的代码
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 我希望那是有用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.