如果是在桌面程序中,只需要
- _context.Log = Console.Out;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
复制代码即可在控制台输出SQL语句。可是在ASP.NET中又该怎么办呢?
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)这时我想起了StringWriter。用它就可以代替Console.Out帮我们接收输出的日志,保存在一个StringBuilder里。
©Å_ùªí(þeâwww.nfhot.comh$»*b)于是构造一个辅助类:
- using System;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Collections.Generic;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Linq;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Web;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.IO;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Text;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- namespace Clowwindy.Models{
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public static class LogHelper{
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public static StringBuilder Log = new StringBuilder();
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public static TextWriter In = new StringWriter(Log);
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public static string GetAllLog(){In.Flush();
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- return Log.ToString();
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public static void Clean(){
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- Log = new StringBuilder();In = new StringWriter(Log);
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
复制代码再添加一个页面log.aspx,用来显示日志:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Log.aspx.cs" Inherits="Clowwindy.Log" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>SQL Log</title></head><body><form id="form1" runat="server"><asp:Button ID="btn_Clean" runat="server" Text="清空" /><div><asp:Literal ID="Literal1" runat="server"></asp:Literal></div></form></body></html>
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
复制代码- using System;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Collections.Generic;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Linq;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Web;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Web.UI;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using System.Web.UI.WebControls;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- using Clowwindy.Models;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- namespace Clowwindy{
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- public partial class Log : System.Web.UI.Page{
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- protected void Page_Load(object sender, EventArgs e){
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- if (Request.UserHostAddress != "127.0.0.1"){
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- Response.End();return;
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- Literal1.Text = LogHelper.GetAllLog().Replace("\n","\n");
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- }
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
- protected void btn_Clean_Click(object sender, EventArgs e){LogHelper.Clean();Literal1.Text = null;}}}
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
复制代码最后在所有new DataContext的地方加上
- public Repository(){_context = new TDataContext();_context.Log = LogHelper.In;}
©Å_ùªí(þeâwww.nfhot.comh$»*b)©Å_ùªí(þeâwww.nfhot.comh$»*b)
©Å_ùªí(þeâwww.nfhot.comh$»*b)
复制代码打开log.aspx,即可看到之前执行的SQL语句。
©Å_ùªí(þeâwww.nfhot.comh$»*b)