/************************************************************************************ * * Umbraco Data Layer * MIT Licensed work * ©2008 Ruben Verborgh * ***********************************************************************************/ using System; using System.Diagnostics; using System.IO; namespace umbraco.DataLayer.Extensions { /// /// Extension to that logs all executed commands to a stream. /// public class Logger : SqlHelperExtension { #region Private Constants /// Format of a line in the log file. private const string LogLineFormat = "#{0} ({1})\r\n{2}\r\n"; #endregion #region Private Fields /// Stream to which the commands are logged. private readonly StreamWriter m_LogStream; /// Parser used to inline query parameters. private readonly SqlParser m_SqlParser = new SqlParser(); #endregion #region Public Constructors and Destructor /// /// Initializes a new instance of the class. /// /// The input stream to which the commands will be logged. public Logger(Stream inputStream) { m_LogStream = new StreamWriter(inputStream); } /// /// Initializes a new instance of the class. /// /// The name of the file in which the commands will be logged. public Logger(string filename) { m_LogStream = new StreamWriter(filename, true); } /// /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~Logger() { try { m_LogStream.Close(); } catch { } } #endregion #region SqlHelperExtension Members /// /// Called when ExecuteScalar is executed. /// /// The SQL helper. /// The command text. /// The SQL parameters. public override void OnExecuteScalar(ISqlHelper sqlHelper, ref string commandText, ref IParameter[] parameters) { AppendToLog(sqlHelper, commandText, parameters); } /// /// Called when ExecuteNonQuery is executed. /// /// The SQL helper. /// The command text. /// The SQL parameters. public override void OnExecuteNonQuery(ISqlHelper sqlHelper, ref string commandText, ref IParameter[] parameters) { AppendToLog(sqlHelper, commandText, parameters); } /// /// Called when ExecuteReader is executed. /// /// The SQL helper. /// The command text. /// The SQL parameters. public override void OnExecuteReader(ISqlHelper sqlHelper, ref string commandText, ref IParameter[] parameters) { AppendToLog(sqlHelper, commandText, parameters); } /// /// Called when ExecuteXmlReader is executed. /// /// The SQL helper. /// The command text. /// The SQL parameters. public override void OnExecuteXmlReader(ISqlHelper sqlHelper, ref string commandText, ref IParameter[] parameters) { AppendToLog(sqlHelper, commandText, parameters); } #endregion #region Private Methods /// /// Appends a command to the log stream. /// /// The SQL helper executing the command. /// The command text. /// The parameters. private void AppendToLog(ISqlHelper sqlHelper, string commandText, IParameter[] parameters) { m_LogStream.WriteLine(LogLineFormat, DateTime.Now, new StackFrame(1).GetMethod().Name, m_SqlParser.ApplyParameters(commandText, parameters)); m_LogStream.Flush(); } #endregion } }