/// <summary> /// Main method to write the line of SQL to the text file /// </summary> /// <param name="data"></param> protected override void RegisterExecutionComplete(ProfilerExecData data) { base.RegisterExecutionComplete(data); StringBuilder sb = new StringBuilder(); string baseformat = "{0:0000000}"; string sql = data.SQL.Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' '); string dur = string.Format("{0:0.0000}", data.Duration.TotalMilliseconds); if (dur.Length < 12) { dur = dur.PadLeft(12); } sb.AppendFormat(baseformat, data.ExecutionID); sb.Append("\t"); sb.Append(dur); sb.Append("\t"); sb.Append(sql); if (data.Parameters != null && data.Parameters.Length > 0) { foreach (object p in data.Parameters) { sb.Append('\t'); if (null == p || p is DBNull) { sb.Append("[NULL]"); } else { sb.Append(p); } } } sb.Append("\r\n"); string path = this.FilePath; path = path.Replace("[Date]", DateTime.Today.ToString("yyyy_MM_dd")) .Replace("[Name]", this.ProfilerName) .Replace("[Connection]", data.Connection); lock (_filelock) { if (!System.IO.File.Exists(path)) { System.IO.File.AppendAllText(path, HeaderString); } System.IO.File.AppendAllText(path, sb.ToString()); } }
public bool TryGetSummary(ProfilerExecData data, out ProfilerExecSummary summary) { if (this.Count == 0) { summary = null; return(false); } else { string key = GetKey(data.Connection, data.SQL); return(this.Dictionary.TryGetValue(key, out summary)); } }
// // public methods // #region public IDBProfileExecData BeginExecution(string connectionName, string sql, params object[] parameters) /// <summary> /// Registers the start of an execution and returns the ProfileData instance which /// encapsulates the required data and identifies it. /// </summary> /// <param name="connectionName"></param> /// <param name="sql"></param> /// <param name="parameters"></param> /// <returns></returns> public IDBProfileExecData BeginExecution(string connectionName, string sql, params object[] parameters) { int id = _nextid++;//should place this in a lock, but the impact of is greater than the chance of clash if (_nextid == int.MaxValue) { _nextid = 0; } ProfilerExecData data = new ProfilerExecData(connectionName, id, sql, _running.Elapsed); data.Parameters = parameters; this.RegisterBeginExecution(id, data); return(data); }
// // support methods // #region private void UpdateExecutionSummary(ProfileData data) /// <summary> /// Updates a ProfileDataSummary by calling RegisterExecution /// </summary> /// <param name="data"></param> private void UpdateExecutionSummary(ProfilerExecData data) { ProfilerExecSummary summary; if (!_summaries.TryGetSummary(data, out summary)) { lock (_summaryLock) { if (!_summaries.TryGetSummary(data, out summary)) { summary = new ProfilerExecSummary(data.Connection, data.SQL); _summaries.Add(summary); } } } summary.RegisterExecution(data); }
// // implementation methods // #region protected override void RegisterExecutionComplete(ProfilerExecData data) /// <summary> /// overrides the base method to write a formatted string to the console. /// </summary> /// <param name="data"></param> protected override void RegisterExecutionComplete(ProfilerExecData data) { int index = 0; int sqllinelength = 70; string front = string.Format("{0:000000}:{1} -> ", data.ExecutionID, data.Connection); string full = data.SQL.Replace('\n', ' ').Replace("\r", "").Replace("\t", ""); string padleft = "".PadLeft(front.Length);//format length of execution id and the colon and space. StringBuilder sb = new StringBuilder(front); while (index < full.Length) { if (index > 0) { sb.Append(padleft); sb.Append("..."); } if (index + sqllinelength > full.Length) { sb.Append(full, index, full.Length - index); } else { sb.Append(full, index, sqllinelength); sb.Append("..."); } if (index == 0) { sb.Append(" Duration:"); sb.Append(data.Duration); //trim the line length to 3 less for the elipsis index += 3; sqllinelength -= 3; } sb.Append("\r\n"); index += sqllinelength; } Console.Write(sb.ToString()); base.RegisterExecutionComplete(data); }
public virtual void RegisterExecution(ProfilerExecData dataexec) { if (null == dataexec) { throw new ArgumentNullException("dataexec"); } else if (!string.Equals(dataexec.SQL, this.SQL, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentOutOfRangeException("dataexec", "The SQL strings do not match"); } long val = dataexec.Duration.Ticks; //update _all list if (_exec > 0) { //second run so init _all list and add the first if (_exec == 1) { _all = new List <long>(); _all.Add(_dur); } _all.Add(val); } //update total + mean _dur += val; _exec += 1; //update min max if (_exec == 1) { _min = val; _max = val; } else { _max = Math.Max(_max, val); _min = Math.Min(_min, val); } }
/// <summary> /// Registers the end of the execution for a specific execution data - this is the ProfileData returned from the BeginExecution method /// </summary> /// <param name="data">The profile data returned from a BeginExecution method</param> public void EndExecution(IDBProfileExecData data) { if (null == data) { throw new ArgumentNullException("data"); } if (!(data is ProfilerExecData)) { throw new InvalidCastException("data"); } ProfilerExecData execdata = data as ProfilerExecData; execdata.Ended = _running.Elapsed; this.RegisterExecutionComplete(execdata); if (this._collect) { this.UpdateExecutionSummary(execdata); } }
public bool Equals(ProfilerExecData data) { return(this.ExecutionID == data.ExecutionID && this.Connection == data.Connection); }
/// <summary> /// Inheritors should override this method if they wish to /// record the completion of a Database execution. Default implementation does nothing /// </summary> /// <param name="data">The data associated with the Execution</param> protected virtual void RegisterExecutionComplete(ProfilerExecData data) { //Inheritors should override, or do nothing }
// // protected virtual methods that should be overriden as required. // #region protected virtual void RegisterBeginExecution(int execid, ProfileData data) /// <summary> /// Inheritors should override this method if they wish to /// record the commencement of an DBExecution. Default implementation does nothing /// </summary> /// <param name="execid"></param> /// <param name="data"></param> protected virtual void RegisterBeginExecution(int execid, ProfilerExecData data) { //Empty method that can be overwritten }