阅读内容 

适用于显示Web项目和DLinq调试信息的小程序

[日期:2008-05-28] 来源:  作者: [字体: ]
    
  在开发控制台(Console)程序时,可以很方便地使用Console.WriteLine()方法显示程序运行过程中的调试信息,例如有时想看某些变量值的变化,或者某些对象的属性值等等。不过在开发Web项目时,就没法使用Console(指黑乎乎的那个窗口)来查看调试信息了,这时虽然可以使用一些成熟的日志工具(例如NLog和Log4Net)来记录程序的调式信息,但是它们的日志没法即时很直观地看到,在调试过程多多少少不够方便。所以自己写了一个简陋的小程序NConsole,让调试消息通过本地网络发送到一个接收端程序,然后在接收端显示文字信息,过程如下:
  
  
  代码的使用方法也很简单,使用方法:
  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/
  
  
    
阅读:
录入:blue1000

推荐 】 【 打印
相关新闻      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:
Advertisement
内容查询


Advertisement