如何在全球范围内进行EF日志SQL查询? [英] How to make EF log sql queries globally?
本文介绍了如何在全球范围内进行EF日志SQL查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何告诉" EF
全局记录查询?我正在阅读此博客文章: EF日志记录,通常介绍如何记录sql
查询.但是对于这个记录器,我仍然有一些疑问.
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 a few questions regarding this logger.
- 我将在哪里放置此行
context.Database.Log = s => logger.Log("EFApp", s);
? - 可以全局设置吗?还是我必须将其放置在我做数据库的任何地方 操作吗?
-
在执行失败"部分,博主写道,而我 引用:
- Where would I need to place this line
context.Database.Log = s => logger.Log("EFApp", s);
? - Can it be globally set? Or do I have to place it everywhere I do DB operations?
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.
如果我不使用context.Database.Log
,是否也将其记录下来?
Will this be logged too if I don't use the context.Database.Log
?
推荐答案
- 每当您希望上下文开始记录时.
- 这似乎是在上下文对象上完成的,因此每次创建新上下文时都应这样做.您可以在构造函数中添加此代码行,以确保始终启用该代码.
- 如果不启用日志记录,它将不会记录日志.
这篇关于如何在全球范围内进行EF日志SQL查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文