开发者

Linq to SQL: How to save the final queries to a log file?

开发者 https://www.devze.com 2023-04-08 10:36 出处:网络
I am looking after an application with lots of LINQ to SQL queries. This is basically SilverLight application with MVC. Some of the data loading is taking quite a bit of time. I wanted to know the exa

I am looking after an application with lots of LINQ to SQL queries. This is basically SilverLight application with MVC. Some of the data loading is taking quite a bit of time. I wanted to know the exact query that is fired on SQL server. Is there any way of storing the generated T-SQL statements to开发者_如何学C some kind of a log file?

Please suggest some other possible ways as well.


LINQ to SQL datacontexts have a property Log that holds a reference to a TextWriter where the text of all the generated SQL queries is written to. You could for example open a StreamWriter for a disk file, then assign it to the Log property of your datacontext. See here for some nice examples: http://damieng.com/blog/2008/07/30/linq-to-sql-log-to-debug-window-file-memory-or-multiple-writers


Your best bet is to use the SQL Server Profiler. This will log all queries to a trace file that you can load up after tracing to see how long queries took etc.

Check out this intro. http://msdn.microsoft.com/en-us/library/ff650699.aspx

Make sure you select the correct event such as Statement Completed.

See this post for further reference

http://weblogs.asp.net/zeeshanhirani/archive/2008/04/18/how-to-use-profiler-with-linq-to-sql-queries.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消