來(lái)自: http://www.yaosansi.com/post/1380.html
記錄LINQ生成的SQL語(yǔ)句是常用的調(diào)試方式,而且能根據(jù)需要來(lái)優(yōu)化LINQ生成的SQL語(yǔ)句,更能了深入的了解LINQ.
DataContext的Log屬性來(lái)將LINQ to SQL生成的SQL語(yǔ)句格式化.
一.控制臺(tái)程序(Console)
dataContext.Log = Console.Out;
二.利用GetCommand方法
dataContext.GetCommand(query).CommandText;
三.使用LINQPad ( 官方網(wǎng)站 )
LINQPad支持C# 3.0 和 Framework 3.5的全部功能:
- LINQ to SQL
- LINQ to Objects
- LINQ to XML
更多介紹請(qǐng)參考 李永京 的 學(xué)習(xí)LINQ工具:LINQPad
下載地址: http://www.albahari.com/LINQPad.exe
四.LINQ to SQL Debug Visualizer
ScottGu的LINQ to SQL Debug Visualizer可以在Debug過(guò)程中查看SQL語(yǔ)句.
介紹: http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
下載: http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip
安裝方法
1. 關(guān)閉 VS2008。
2. 將壓縮包中的 SqlServerQueryVisualizer.dll 拷貝到 \Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers。
3. 重啟 VS2008 即可。
五.DebuggerWriter工具類
由于Console.Out方法在ASP.NET程序不起作用.
Kris Vandermotten 已經(jīng)創(chuàng)建好了一個(gè)這個(gè)工具類, 你只要使用這樣的語(yǔ)法:
MyDataContext db =
new
MyDataContext();
db.Log =
new
DebuggerWriter();
?
asp.net可以選擇將Log信息直接發(fā)送到Debug的輸出窗口.
源碼:
using
?System;
using
?System.Diagnostics;
using
?System.Globalization;
using
?System.IO;
using
?System.Text;
namespace
?Vandermotten.Diagnostics
{
????
///
?
<summary>
????
///
?Implements?a?
<see?cref="TextWriter"/>
?for?writing?information?to?the?debugger?log.
????
///
?
</summary>
????
///
?
<seealso?cref="Debugger.Log"/>
????
public
?
class
?DebuggerWriter?:?TextWriter
????
{
????????
private
?
bool
?isOpen;
????????
private
?
static
?UnicodeEncoding?encoding;
????????
private
?
readonly
?
int
?level;
????????
private
?
readonly
?
string
?category;
????????
///
?
<summary>
????????
///
?Initializes?a?new?instance?of?the?
<see?cref="DebuggerWriter"/>
?class.
????????
///
?
</summary>
????????
public
?DebuggerWriter()
????????????:?
this
(
0
,?Debugger.DefaultCategory)
????????
{
????????}
????????
///
?
<summary>
????????
///
?Initializes?a?new?instance?of?the?
<see?cref="DebuggerWriter"/>
?class?with?the?specified?level?and?category.
????????
///
?
</summary>
????????
///
?
<param?name="level">
A?description?of?the?importance?of?the?messages.
</param>
????????
///
?
<param?name="category">
The?category?of?the?messages.
</param>
????????
public
?DebuggerWriter(
int
?level,?
string
?category)
????????????:?
this
(level,?category,?CultureInfo.CurrentCulture)
????????
{
????????}
????????
///
?
<summary>
????????
///
?Initializes?a?new?instance?of?the?
<see?cref="DebuggerWriter"/>
?class?with?the?specified?level,?category?and?format?provider.
????????
///
?
</summary>
????????
///
?
<param?name="level">
A?description?of?the?importance?of?the?messages.
</param>
????????
///
?
<param?name="category">
The?category?of?the?messages.
</param>
????????
///
?
<param?name="formatProvider">
An?
<see?cref="IFormatProvider"/>
?object?that?controls?formatting.
</param>
????????
public
?DebuggerWriter(
int
?level,?
string
?category,?IFormatProvider?formatProvider)
????????????:?
base
(formatProvider)
????????
{
????????????
this
.level?
=
?level;
????????????
this
.category?
=
?category;
????????????
this
.isOpen?
=
?
true
;
????????}
????????
protected
?
override
?
void
?Dispose(
bool
?disposing)
????????
{
????????????isOpen?
=
?
false
;
????????????
base
.Dispose(disposing);
????????}
????????
public
?
override
?
void
?Write(
char
?value)
????????
{
????????????
if
?(
!
isOpen)
????????????
{
????????????????
throw
?
new
?ObjectDisposedException(
null
);
????????????}
????????????Debugger.Log(level,?category,?value.ToString());
????????}
????????
public
?
override
?
void
?Write(
string
?value)
????????
{
????????????
if
?(
!
isOpen)
????????????
{
????????????????
throw
?
new
?ObjectDisposedException(
null
);
????????????}
????????????
if
?(value?
!=
?
null
)
????????????
{
????????????????Debugger.Log(level,?category,?value);
????????????}
????????}
????????
public
?
override
?
void
?Write(
char
[]?buffer,?
int
?index,?
int
?count)
????????
{
????????????
if
?(
!
isOpen)
????????????
{
????????????????
throw
?
new
?ObjectDisposedException(
null
);
????????????}
????????????
if
?(buffer?
==
?
null
?
||
?index?
<
?
0
?
||
?count?
<
?
0
?
||
?buffer.Length?
-
?index?
<
?count)
????????????
{
????????????????
base
.Write(buffer,?index,?count);?
//
?delegate?throw?exception?to?base?class
????????????}
????????????Debugger.Log(level,?category,?
new
?
string
(buffer,?index,?count));
????????}
????????
public
?
override
?Encoding?Encoding
????????
{
????????????
get
????????????
{
????????????????
if
?(encoding?
==
?
null
)
????????????????
{
????????????????????encoding?
=
?
new
?UnicodeEncoding(
false
,?
false
);
????????????????}
????????????????
return
?encoding;
????????????}
????????}
????????
public
?
int
?Level
????????
{
????????????
get
?
{?
return
?level;?}
????????}
????????
public
?
string
?Category
????????
{
????????????
get
?
{?
return
?category;?}
????????}
????}
}
六.將LINQ to SQL生成的SQL語(yǔ)句寫(xiě)入日志文件
DataContext.Log是System.IO.TextWriter類型,所以你可以用以下的方法來(lái)做.
StreamWriter sw =
new
StreamWriter(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"log.txt"
));
dBLinqDataContext.Log = sw;
var query = dataContext.Customers.Single<Customer>(c=>c.CustomerID.Contains(
"s"
))
.Skip(0).Take(10).ToList();
sw.Flush();
sw.Close();
但以上方法有個(gè)缺點(diǎn),就是需要在每個(gè)實(shí)現(xiàn)的方法中都寫(xiě)這么多代碼.使用起來(lái)太不方便.參照dataContext.Log = Console.Out的表現(xiàn)形式
由是有了FileLog類.(當(dāng)然,FileLog類除了此功能還有一些基本的記錄日志的方法)
使用時(shí)直接dataContext.Log = Yaosansi.IO.FileLog.Out;即可. 默認(rèn)會(huì)在桌面上生成一個(gè)名叫UnNameFile.txt的文件.
當(dāng)然如果你不想使用默認(rèn)的文件名和路徑也可以使用dataContext.Log =new Yaosansi.IO.FileLog("FileName")的方式.
下面是FileLog類的源碼:
//
原文:
http://www.yaosansi.com/post/1380.html
using
?System;
using
?System.Collections.Generic;
using
?System.Text;
using
?System.IO;
namespace
?Yaosansi.IO
{
????
///
?
<summary>
????
///
?文件操作
????
///
?
</summary>
????
public
?
class
?FileLog?:?TextWriter
????
{
????????
構(gòu)造函數(shù)
????????
重寫(xiě)TextWriter
????????
屬性
????????
方法
????}
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

