来自:http://www.yaosansi.com/post/1380.html
记录LINQ生成的SQL语句是常用的调试方式,而且能根据需要来优化LINQ生成的SQL语句,更能了深入的了解LINQ.
DataContext的Log属性来将LINQ to SQL生成的SQL语句格式化.
一.控制台程序(Console)
dataContext.Log = Console.Out;
二.利用GetCommand方法
dataContext.GetCommand(query).CommandText;
三.使用LINQPad (官方网站)
LINQPad支持C# 3.0 和 Framework 3.5的全部功能:
- LINQ to SQL
- LINQ to Objects
- LINQ to XML
更多介绍请参考李永京的学习LINQ工具:LINQPad
下载地址:http://www.albahari.com/LINQPad.exe
四.LINQ to SQL Debug Visualizer
ScottGu的LINQ to SQL Debug Visualizer可以在Debug过程中查看SQL语句.
介绍:http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
下载:http://www.scottgu.com/blogposts/linqquery/SqlServerQueryVisualizer.zip
安装方法
1. 关闭 VS2008。
2. 将压缩包中的 SqlServerQueryVisualizer.dll 拷贝到 \Program Files\Microsoft Visual Studio 9.0\Common7\Packages\Debugger\Visualizers。
3. 重启 VS2008 即可。
五.DebuggerWriter工具类
由于Console.Out方法在ASP.NET程序不起作用.
Kris Vandermotten 已经创建好了一个这个工具类, 你只要使用这样的语法:
MyDataContext db = new MyDataContext();
db.Log = new DebuggerWriter();
asp.net可以选择将Log信息直接发送到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语句写入日志文件
DataContext.Log是System.IO.TextWriter类型,所以你可以用以下的方法来做.
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();
但以上方法有个缺点,就是需要在每个实现的方法中都写这么多代码.使用起来太不方便.参照dataContext.Log = Console.Out的表现形式
由是有了FileLog类.(当然,FileLog类除了此功能还有一些基本的记录日志的方法)
使用时直接dataContext.Log = Yaosansi.IO.FileLog.Out;即可. 默认会在桌面上生成一个名叫UnNameFile.txt的文件.
当然如果你不想使用默认的文件名和路径也可以使用dataContext.Log =new Yaosansi.IO.FileLog("FileName")的方式.
下面是FileLog类的源码:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)