这是今天课堂上的一个例子,我们实现了一个简单的SQLServerTraceListener
class SQLServerTraceListener : TraceListener { public override void Write(string message) { using (SqlConnection conn = new SqlConnection("server=(local);database=TraceDatabase;Integrated security=true;")) { using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "INSERT INTO Logs(Message) VALUES(@message)"; cmd.Parameters.Add( new SqlParameter( "@message", message)); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } } public override void WriteLine(string message) { Write(message); } }