/// <summary> /// like traceEvent except it uses a stopwatch 'timer' to write formatted time after the message [{1:0}ms]. /// Usually used to signal in the log how long an operation took. /// </summary> /// <param name="trace"></param> /// <param name="elapsed"></param> /// <param name="eventType"></param> /// <param name="message"></param> /// <param name="args"></param> static void traceEvent(this TraceSource trace, TimeSpan elapsed, LogEventType eventType, string message, params object[] args) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var timespan = ShortTimeSpan.FromSeconds(elapsed.TotalSeconds); if (sb == null) { sb = new System.Text.StringBuilder(); } sb.Clear(); if (args.Length == 0) { sb.Append(message); } else { sb.AppendFormat(message, args); } sb.Append(" ["); timespan.ToString(sb); sb.Append("]"); trace.TraceEvent(eventType, 0, sb.ToString()); }
void printSummary(TestStepRun run, int maxIndent, int idx, ILookup <Guid, TestStepRun> lookup) { int indentcnt = stepRunIndent(run); string indent = new String(' ', indentcnt * 2); string inverseIndent = new String(' ', maxIndent * 2 - indentcnt * 2); var(timeString, unit) = ShortTimeSpan.FromSeconds(run.Duration.TotalSeconds).ToStringParts(); string v = run.Verdict == Verdict.NotSet ? "" : run.Verdict.ToString(); var name = run.TestStepName; summaryLog.Info("{4} {0,-43} {5}{1,5:0} {7,-4}{2,-7}", name, timeString, v, idx, indent, inverseIndent, 0, unit); int idx2 = 0; foreach (TestStepRun run2 in lookup[run.Id]) { printSummary(run2, maxIndent, idx2, lookup); } }
/// <summary> /// Prints the summary. /// </summary> public void PrintSummary() { if (planRun == null) { return; //Something very wrong happened. In this case the use will be informed of an error anyway. } bool hasOtherVerdict = planRun.Verdict != Verdict.Pass && planRun.Verdict != Verdict.NotSet; ILookup <Guid, TestStepRun> parentLookup = null; int maxIndent = 0; if (stepRuns != null) { parentLookup = stepRuns.Values.ToLookup(v => v.Parent); Func <Guid, int> getMaxIndent = null; getMaxIndent = guid => 1 + parentLookup[guid].Select(step => getMaxIndent(step.Id)).DefaultIfEmpty(0).Max(); maxIndent = getMaxIndent(planRun.Id); } int maxVerdictLength = hasOtherVerdict ? planRun.Verdict.ToString().Length : 0; if (stepRuns != null) { foreach (TestStepRun run in parentLookup[planRun.Id]) { int max = getMaxVerdictLength(run, maxVerdictLength, parentLookup); if (max > maxVerdictLength) { maxVerdictLength = max; } } } int addPadLength = maxIndent + (int)Math.Round(maxVerdictLength / 2.0); int maxLength = -1; Func <string, int, string> formatSummaryHeader = (message, indentLength) => { string indent = new String('-', indentLength + 3); StringBuilder sb = new StringBuilder(indent); sb.Append(message); sb.Append(indent); maxLength = sb.Length; return(sb.ToString()); }; Func <string, string> formatSummary = (message) => { int fillLength = (int)Math.Floor((maxLength - message.Length) / 2.0); StringBuilder sb = new StringBuilder(new string('-', fillLength)); sb.Append(message); sb.Append('-', maxLength - sb.Length); return(sb.ToString()); }; summaryLog.Info(formatSummaryHeader(String.Format(" Summary of test plan started {0} ", planRun.StartTime), addPadLength)); if (stepRuns != null) { int idx = 0; foreach (TestStepRun run in parentLookup[planRun.Id]) { printSummary(run, maxIndent, idx, parentLookup); } } else { summaryLog.Warning("Test plan summary skipped. Summary contains too many steps ({0}).", stepRunLimit); } summaryLog.Info(formatSummary(separator)); if (!hasOtherVerdict) { summaryLog.Info(formatSummary(String.Format(" Test plan completed successfully in {0,6} ", ShortTimeSpan.FromSeconds(planRun.Duration.TotalSeconds).ToString()))); } else { summaryLog.Info(formatSummary(String.Format(" Test plan completed with verdict {1} in {0,6} ", ShortTimeSpan.FromSeconds(planRun.Duration.TotalSeconds).ToString(), planRun.Verdict))); } }