使用context.Database.登录MVC Web应用 [英] Using context.Database.Log in MVC web app
问题描述
使用指南此处,我正在尝试记录我的MVC Web应用程序生成的SQL.
Using the guide here, I'm trying to log the SQL generated by my MVC web application.
指南使用以下行:
context.Database.Log = Console.Write;
这显然不适用于Web应用程序.该指南介绍了Log属性:
Which obviously doesn't work with a web application. The guide described the Log property:
DbContext.Database.Log属性可以设置为任何代理 需要一个字符串的方法.
The DbContext.Database.Log property can be set to a delegate for any method that takes a string.
这是我不理解的,代表们只是让我感到困惑.如何使用此EF6功能?
And it's this I don't understand, delegates just confuse me. How can I use this EF6 feature?
推荐答案
使用委托可以让您编写任何使用字符串的函数.作为对文件的非常简单的日志记录,您可以执行以下操作:
Using a delegate allows you to do write any function taking a string. As a very simple logging to a file, you could do the following:
context.Database.Log = message => File.AppendText("C:\\mylog.txt").WriteLine(message);
在网络环境中,您可能希望使用跟踪记录此信息:
In a web environment, you may wish to use Trace to log this information:
context.Database.Log = message => Trace.WriteLine(message);
这篇关于使用context.Database.登录MVC Web应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!