In a desktop program
_ Context. Log = Console. Out;
You can output SQL statements on the console. But what should I do in ASP. NET?
Now I think of StringWriter. Instead of Console. Out, you can use it to receive output logs and store them in a StringBuilder.
Construct a helper class:
Using System; using System. collections. generic; using System. linq; using System. web; using System. IO; using System. text; namespace Clowwindy. models {public static class LogHelper {public static StringBuilder Log = new StringBuilder (); public static TextWriter In = new StringWriter (Log); public static string GetAllLog () {In. flush (); return Log. toString () ;}public static void Clean () {Log = new StringBuilder (); In = new StringWriter (Log );}}}
Add a page log. aspx to display the log:
<% @ 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">
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using Clowwindy. models; namespace Clowwindy {public partial class Log: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {if (Request. userHostAddress! = "127.0.0.1") {Response. end (); return;} Literal1.Text = LogHelper. getAllLog (). replace ("\ n", "\ n <br/>");} protected void btn_Clean_Click (object sender, EventArgs e) {LogHelper. clean (); Literal1.Text = null ;}}}
Add _ context. Log = LogHelper. In to all new DataContext:
Public Repository () {_ context = new TDataContext (); _ context. Log = LogHelper. In ;}
Open log. aspx and you will see the previously executed SQL statement.