如果是在桌面程序中,只需要
_context.Log = Console.Out;
即可在控制台输出SQL
语句。可是在
ASP.
NET中又该怎么办呢?
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t这时我想起了StringWriter。用它就可以代替Console.Out帮我们接收输出的日志,保存在一个StringBuilder里。
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t于是构造一个辅助类:
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Collections.Generic;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Linq;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Web;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.IO;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Text;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
namespace Clowwindy.Models{oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public static class LogHelper{oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public static StringBuilder Log = new StringBuilder();oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public static TextWriter In = new StringWriter(Log);oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public static string GetAllLog(){In.Flush();oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
return Log.ToString();oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public static void Clean(){oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
Log = new StringBuilder();In = new StringWriter(Log);oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t再添加一个页面log.aspx,用来显示日志:
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
<%@ 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>
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Collections.Generic;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Linq;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Web;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Web.UI;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using System.Web.UI.WebControls;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
using Clowwindy.Models;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
namespace Clowwindy{oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public partial class Log : System.Web.UI.Page{oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
protected void Page_Load(object sender, EventArgs e){oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
if (Request.UserHostAddress != "127.0.0.1"){oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
Response.End();return;oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
Literal1.Text = LogHelper.GetAllLog().Replace("\n","\n");oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
}oV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
protected void btn_Clean_Click(object sender, EventArgs e){LogHelper.Clean();Literal1.Text = null;}}}
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t最后在所有new DataContext的地方加上
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t
public Repository(){_context = new TDataContext();_context.Log = LogHelper.In;}
oV¯¾ANwww.nfhot.comÜù 6¨FÞ±toV¯¾ANwww.nfhot.comÜù 6¨FÞ±t打开log.aspx,即可看到之前执行的SQL语句。