Monday, May 26, 2008

SQL Execute Trace Helper Class

http://focuswindows.blogspot.com/2008/01/sql-executing-monitor-code.html
该版本可能不是最新的, 最新的代码应该是下面:



namespace LiuHarry.Utils.DB
{
using LiuHarry.Utils.Foundation;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;

internal class SqlExecuteTracer : IDisposable
{
private bool disposed = false;
private string m_ErrorLogFile;
private readonly string m_ErrorLogFileSuffix = "SqlError.txt";
private bool m_LogSqlExecute = false;
private string m_SqlLogFile;
private readonly string m_SqlLogFileSuffix = "SqlLog.txt";
private TextWriterTraceListener m_SqlLogListener;

public SqlExecuteTracer()
{
string assemblyFileWithoutExt = this.GetAssemblyFileWithoutExt();
this.m_ErrorLogFile = assemblyFileWithoutExt + "_" + this.m_ErrorLogFileSuffix;
this.m_SqlLogFile = assemblyFileWithoutExt + "_" + this.m_SqlLogFileSuffix;
}

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (!this.disposed && this.m_LogSqlExecute)
{
this.m_SqlLogListener.Close();
}
this.disposed = true;
}

private string GetAssemblyFileWithoutExt()
{
string location = Assembly.GetExecutingAssembly().Location;
return (Path.GetDirectoryName(location) + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(location));
}

private string GetSqlFromCommand(IDbCommand cmd)
{
string commandText = cmd.CommandText;
if (cmd.Parameters.Count <= 0)
{
return commandText;
}
DelphiStrList list = new DelphiStrList();
foreach (IDbDataParameter parameter in cmd.Parameters)
{
if (parameter.Value != null)
{
list.Add(parameter.ParameterName + "=" + parameter.Value.ToString());
}
else
{
list.Add(parameter.ParameterName + "=NULL");
}
}
return (commandText + Environment.NewLine + list.MakeItemsToText(Environment.NewLine));
}

public void TraceErrorSqlToLog(IDbCommand cmd, string ErrorMessage)
{
using (TraceListener listener = new TextWriterTraceListener(this.m_ErrorLogFile))
{
string sqlFromCommand = this.GetSqlFromCommand(cmd);
this.TraceSqlToLogListener(sqlFromCommand, ErrorMessage, listener);
listener.Close();
}
}

public void TraceErrorSqlToLog(string sql, string ErrorMessage)
{
using (TraceListener listener = new TextWriterTraceListener(this.m_ErrorLogFile))
{
this.TraceSqlToLogListener(sql, ErrorMessage, listener);
listener.Close();
}
}

public void TraceSqlToLog(IDbCommand cmd)
{
if (this.m_LogSqlExecute)
{
string sqlFromCommand = this.GetSqlFromCommand(cmd);
this.TraceSqlToLogListener(sqlFromCommand, null, this.m_SqlLogListener);
}
}

public void TraceSqlToLog(string sql)
{
if (this.m_LogSqlExecute)
{
this.TraceSqlToLogListener(sql, null, this.m_SqlLogListener);
}
}

private void TraceSqlToLogListener(string sql, string ErrorInfo, TraceListener listener)
{
listener.WriteLine("===================================");
listener.WriteLine(DateTime.Now.ToString());
if (!string.IsNullOrEmpty(ErrorInfo))
{
listener.WriteLine("Error Info:");
listener.WriteLine(ErrorInfo);
listener.WriteLine("SQL:");
}
listener.WriteLine(sql);
listener.WriteLine("===================================");
listener.WriteLine("");
listener.Flush();
}

public bool LogSqlExecute
{
get
{
return this.m_LogSqlExecute;
}
set
{
this.m_LogSqlExecute = value;
if (this.m_LogSqlExecute && (this.m_SqlLogListener == null))
{
this.m_SqlLogListener = new TextWriterTraceListener(this.m_SqlLogFile);
}
}
}
}
}

2 comments:

Admirador said...
This comment has been removed by the author.
Admirador said...

thx,

where is using LiuHarry.Utils.Foundation ?

greets