Sending the LINQ To SQL log to the debugger output window
来源:互联网 发布:js面向对象例子 编辑:程序博客网 时间:2024/05/23 13:45
转自:http://blogs.u2u.be/kris/post/2007/03/14/Sending-the-LINQ-To-SQL-log-to-the-debugger-output-window.aspx
---------------------------------------------------------------------------------------------------------------------------------------------------------------
I was playing with LINQ To SQL (again) this evening, and I thought: why can't I see the generated SQL statements in my debugger output window?
The DataContext.Log property is a great help to learn writing LINQ To SQL queries. All you need to do is assign a TextWriter to it, and you get to see al the SQL statements generated by the data context. But where do you send them to? Console.Out is an option, but you probably don't want that in a release build. Furthermore, it doesn't quite work for Windows services or ASP.NET applications. That's why I wrote DebuggerWriter, an implementation of TextWriter that writes to the debugger log.
All you need to do to use it is:
MyDataContext db = new MyDataContext();
db.Log = new DebuggerWriter();
Here's the code:
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; }
}
}
}
Enjoy!
- Sending the LINQ To SQL log to the debugger output window
- use dynamic sql to output the result
- XMLHttpRequest - Sending a Request to the Server
- Sending the User to Another App
- Sending the User to Another App
- Sending the User to Another App
- How to Use the Visual C++ DEBUGGER
- How to: Launch the Debugger Automatically
- [Android] waiting for the debugger to attach
- UIViewAlertForUnsatisfiableConstraints to catch this in the debugger
- Failed sending reply to debugger: Broken pipe
- Loadrunner连接负载机失败,提示"Failed to connect to the load generator.Check the output window for more detail
- loadrunner 连接负载机时"Failed to connect to the load generator.Check the output window for more details"
- Netconsole to capture the log
- Using Reporting Services to Search the SQL Server Log
- Output error file to the following location
- how to control the log4j output
- 92 Examine the output of the query that you executed to list the objects in the recycle bin: SQL> SE
- 关于存储过程return 和 output 获取
- 揭秘IT人才特点:中美印日四国程序员比较
- 十年高深程序员的现身说法--善待职业和人生
- 常用正则表达式 很好的
- SQL远程连接数据库进行数据操作
- Sending the LINQ To SQL log to the debugger output window
- asp.net(C#)读取pdf文件
- C#中的定制特性(Attributes)
- 面向接口编程详解(一)——思想基础
- Zend Debugger安装小记
- Chrome浏览器评测相关
- SharePoint2007 OS 64x站点无法访问 500错误
- XCode 真机测试发布时产生The executable was signed with invalid entitlements.解决办法之一
- 重装windows后重新引导linux方法汇总