来源:uuBox.net 开发日记 - 博客园
在开发控制台(Console)和WinForm的程序时,可以很方便地使用Console.WriteLine()方法显示程序运行过程中的调试信息,例如有时想看某些变量值的变化,或者某些对象的属性值等等。不过在开发Web项目时,就没法使用Console来查看调试信息了,这时虽然可以使用一些成熟的日志工具(例如NLog和Log4Net)来记录程序的调式信息,但是它们的日志没法即时被看到,在调试过程多多少少不够方便。所以自己写了一个简陋的小程序NConsole,让调试消息通过本地网络发送到一个接收端程序,然后在接收端显示文字信息,过程如下:

消息是通过UDP协议来发送的,之所以使用udp协议,主要是因为它的速度比较快(省略了tcp的握手过程),并且当接收端程序不运行的时候,发送端也不会产生错误。
上图中的NConsoleReceiver就是接收端程序,运行的效果图如下:

代码的使用方法也很简单,使用方法:
1、先运行服务端 NConsoleReceiver.exe
2、添加NConsole.cs 文件到待调试的项目
3、添加 Using Doms.NConsoleClient 到类的 using 段。
4、在Web项目调试时这样调用:
NConsole.WriteLine("Hello World");
5、如果想查看Dlinq的调试信息(即它在后台产生的SQL语句),可以这样使用:
DataContext dc = new ....
dc.Log = NConsole.Logger;
下面粘贴程序的代码出来,其中接收端就一个 Program.cs 文件,而发送端就就一个 NConsole.cs 文件。
--------------------------------------------------------------------------------
接收端:Programs.cs
using System;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Doms.NConsoleReceiver
{
/**//// <summary>
/// 简单的udp控制台消息接收器
/// By: Kwanhong Young, 2008-5, from: http://www.cnblogs.com/uubox
/// NewBSD License
/// </summary>
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Start();
}
public void Start()
{
Console.WriteLine("NConsole 接收器正在运行 ……\n");
Console.WriteLine("如果Windows防火墙提示是否阻止本程序,请点击“解除阻止”,此程序并非木马 :-)");
Console.WriteLine("要退出程序请按 <Ctrl+C>。\n\n");
int port = 12345; //NConsole使用的网络端口号
IPEndPoint ep = new IPEndPoint(IPAddress.Any,port); //监听所有本机IP地址
UdpClient socket = new UdpClient(port);
while (true)
{
byte[] data = socket.Receive(ref ep); //接受数据
string content = Encoding.Default.GetString(data); //使用系统默认编码将数据转换成字符串
//改变控制台字体颜色
if (content[0] == '!')
Console.ForegroundColor = ConsoleColor.Red;
else if (content[0] == '*')
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(content);
}
}
}//end class
}
--------------------------------------------------------------------------------
发送端 NConsole.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
/**//////////////////////////////////////////////////////////////////////////////////////
//NConsole:
// 简单的udp控制台,适用于Web、DLinq等项目开发过程中显示调试信息
//
/**//////////////////////////////////////////////////////////////////////////////////////
//使用方法:
//先运行服务端 NConsoleReceiver.exe
//
//添加本cs文件到项目
//添加 Using Doms.NConsoleClient 到 using 段。
//调试时这样调用:
// NConsole.WriteLine("Hello World");
//调式 Dlinq 的 DataContext 的 Log 示例:
// DataClasses1DataContext dc = new DataClasses1DataContext();
// dc.Log = NConsole.Logger;
/**//////////////////////////////////////////////////////////////////////////////////////
namespace Doms.NConsoleClient
{
/**//// <summary>
/// 简单的udp控制台客户端
/// By: Kwanhong Young, 2008-5, from: http://www.cnblogs.com/uubox
/// NewBSD License
/// </summary>
public class NConsole
{
public static readonly NConsoleSender Logger = new NConsoleSender();
public static void Write(string value)
{
NConsole.Logger.Write(value);
}
public static void WriteLine(string format, params object[] args)
{
NConsole.Logger.Write(string.Format(format, args) + "\n");
}
public static void Warning(string value)
{
//用于显示警告的消息
NConsole.Logger.Write("*" + value + "\n");
}
public static void Error(string value)
{
//用于显示错误的消息
NConsole.Logger.Write("!" + value + "\n");
}
}//end class
public class NConsoleSender:TextWriter
{
private string _ipaddr = "127.0.0.1"; //NConsole接收器所在的计算机的IP地址
private int _port = 12345; //NConsole使用的网络端口号
private IPEndPoint _ep;
private UdpClient _socket;
public NConsoleSender()
{
_socket = new UdpClient();
_ep = new IPEndPoint(IPAddress.Parse(_ipaddr), _port);
}
public override void Write(string value)
{
//发送消息给NConsole接收器
byte[] data = Encoding.Default.GetBytes(value);
_socket.Send(data, data.Length, _ep);
}
public override void WriteLine(string value)
{
this.Write(value + "\n");
}
public override Encoding Encoding
{
get { return Encoding.Default; }
}
}//end class
}
源代码也可以从这里下载:(vs2008格式)
http://csprogram.uubox.net/browse.u/nconsole/