public IList<OrderedOutput> HandleMethodExit(MethodExit logEntry, int lineNo, ILogFilters filters, Func<DateTime, string> lineBuilder, bool displayEnabled)
        {
            if (!displayEnabled)
            {
                // method is not actually displayed, we are tracking the nesting level for other items displayed such as logs, exceptions, etc
                _currentNestedLevel--;
                return null;
            }

            if ((_lastMethodEntry == null) || (_lastMethodEntry.Count <= 0))
                throw new InvalidOperationException(String.Format("No related Method Entry log has been set for '{0}' at line {1:0000} - there could be a problem with the yalf logs."
                                                                , logEntry.MethodName, lineNo));
            if (_lastMethodEntry.Peek().MethodName != logEntry.MethodName)
                throw new InvalidOperationException(String.Format("The method exit log '{1}' has a different name than the current method entry log '{0}' at line {2:0000} - there could be a problem with the yalf logs."
                                                                , _lastMethodEntry.Peek().MethodName, logEntry.MethodName, lineNo));

            var currentMethodEntry = _lastMethodEntry.Pop();
            _currentNestedLevel--;

            int indexOfEntryToUpdate = FindLastIndexOf(currentMethodEntry);
            if (indexOfEntryToUpdate < 0)
                throw new InvalidOperationException(String.Format("Could not find the method [{0}] in the current ordered buffer.  This probably means there is an error in the processing logic, or the yalf file is corrupt.",
                                                                        currentMethodEntry.MethodName));

            var currentItem = _orderedBuffer[indexOfEntryToUpdate];
            _orderedBuffer[indexOfEntryToUpdate] = new LogEntryTracker(currentItem.Level, currentItem.RelatedEntry, lineBuilder(currentMethodEntry.Time));

            if (_lastMethodEntry.Count > 0)
                return null;

            _currentNestedLevel = 0;
            return this.PrepareOutputBuffer();
        }
        /// <summary>
        /// Collates nested log calls and only returns an list of formatted log strings when a top level method exit log entry is envcountered
        /// </summary>
        /// <remarks>
        /// <para>This is required as, due to the nature of the single line formatter, nested logs are returned in the wrong order with the normal <see cref="FormatMethodExit"/> method.</para>
        /// <para>The indent must still be applied to the strings in the returned list in the <see cref="ILogOutputHandler"/>.  The first item will be the top level method call.</para>
        /// </remarks>
        public IList<OrderedOutput> FormatMethodExitForSingleLineOutput(int threadId, int level, int lineNo, MethodExit logEntry, ILogFilters filters, bool displayEnabled)
        {
            Func<DateTime, String> lineBuilder = null;

            if (displayEnabled)
                lineBuilder = this.CreateMethodExitLineGenerator(logEntry, filters);

            return _delayedService.HandleMethodExit(logEntry, lineNo, filters, lineBuilder, displayEnabled);
        }
        public void HandleMethodExit(MethodExit entry, int indentLevel, bool displayEnabled)
        {
            if (this.Formatter is ISingleLineOutputLogFormatter)
            {
                this.ManageNestedCallsForSingleLineFormats(entry, indentLevel, displayEnabled);
                return;
            }

            this.AddLine(this.Formatter.FormatMethodExit(this.CurrentThreadId, indentLevel, ++_lineNumber, entry, this.Filters, displayEnabled), indentLevel);
        }
Exemple #4
0
        public string FormatMethodExit(int threadId, int level, int lineNo, MethodExit logEntry, ILogFilters filters, bool displayEnabled)
        {
            if ((filters.HideExitMethodLogs) || (!displayEnabled))
                return null;

            var returnValue = (logEntry.ReturnRecorded && !filters.HideMethodReturnValue) ? "(" + logEntry.ReturnValue + ")" : "()";
            if (filters.HideMethodDuration)
                return string.Format("[Exit] {0}{1}", logEntry.MethodName, returnValue);

            return string.Format("[Exit] {0}{1} duration {2:0.####}ms", logEntry.MethodName, returnValue, logEntry.ElapsedMs);
        }
        public void FormatMethodExit_NoEntryMethodSupplied_ExceptionIsThrown()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new SingleLineFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            var relatedEntry = new MethodEntry(1, "Yalf.DifferentMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act, Assert
            var outputText = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);
        }
 public string FormatMethodExit(int threadId, int level, int lineNo, MethodExit logEntry, ILogFilters filters, bool displayEnabled)
 {
     throw new NotImplementedException(String.Format("{0} does not need to immplement this method, use the ISingleLineOutputLogFormatter.FormatMethodExitForSingleLineOutput interface method so the calls are in the right order.", this.GetType().Name));
 }
 private Func<DateTime, string> CreateLineBuilderGenerator(int threadId, int level, MethodExit logEntry, ILogFilters filters)
 {
     var returnValue = (logEntry.ReturnRecorded && !filters.HideMethodReturnValue) ? logEntry.ReturnValue : "";
     return startTime => BuildOutputLine("Method", logEntry.MethodName, returnValue, startTime, logEntry.ElapsedMs, level, threadId);
 }
        public void FormatMethodExit_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = "Yalf,Method,Yalf.TestMethod,returnVal,22:22:31.678,345,2,1";

            var relatedEntry = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act
            var orderedOutput = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);

            // Assert
            Assert.That(orderedOutput.Count, Is.Not.EqualTo(0), "Expected one string to be returned");
            Assert.That(orderedOutput[0].FormattedLine, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void HandleMethodExit_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new DefaultOutputHandler(filters);
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = String.Concat("".PadLeft(2, output.Formatter.IndentChar), "[Exit] Yalf.TestMethod(returnVal) duration 345ms");
            output.Initialise();

            // Act
            output.HandleMethodExit(entry, 1, true);
            output.Complete();

            // Assert
            var outputText = output.GetReport();
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        private void ManageNestedCallsForSingleLineFormats(MethodExit entry, int indentLevel, bool displayEnabled)
        {
            var output = (this.Formatter as ISingleLineOutputLogFormatter).FormatMethodExitForSingleLineOutput(this.CurrentThreadId, indentLevel, ++_lineNumber, entry, this.Filters, displayEnabled);
            if (output == null)
                return;

            foreach (OrderedOutput outputLine in output)
            {
                this.AddLine(outputLine.FormattedLine, indentLevel + outputLine.Level);
            }
        }
        public void FormatMethodExit_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DefaultFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = "[Exit] Yalf.TestMethod(returnVal) duration 345ms";

            // Act
            var outputText = formatter.FormatMethodExit(22, 1, 33, entry, filters, true);

            // Assert
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        private Func<DateTime, string> CreateMethodExitLineGenerator(MethodExit logEntry, ILogFilters filters)
        {
            var returnValue = (logEntry.ReturnRecorded && !filters.HideMethodReturnValue) ? "(" + logEntry.ReturnValue + ")" : "()";
            var duration = (filters.HideMethodDuration) ? "" : string.Format(" duration {0:0.####}ms", logEntry.ElapsedMs);

            return startTime =>
                {
                    var timestamp = (filters.HideTimeStampInMethod) ? "" : string.Concat(" started ", (startTime.ToString(DateTimeFormat)));
                    return String.Concat(logEntry.MethodName, returnValue, timestamp, duration);
                };
        }
        public void HandleThread_ValidLogWithBlankThreadName_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new ThreadCollectionOutputHandler(filters);

            var entry1 = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            var entry2 = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            int threadId = 22;
            var entry = new ThreadData(threadId, String.Empty, new BaseEntry[] { entry1, entry2 });
            output.Initialise();

            // Act
            output.HandleThread(entry);
            output.Complete();

            // Assert
            Assert.That(output.CurrentThreadId, Is.EqualTo(threadId), "Not the expected thread id for the current thread.");

            var outputText = output.GetThreadEntries();
            Assert.That(outputText.Count, Is.EqualTo(1), "Expected one thread collection");
            Assert.That(outputText[threadId].Count, Is.EqualTo(0), "Expected the thread to have no log entries");
        }
        public void HandleMethodExit_ValidLog_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new ThreadCollectionOutputHandler(filters);
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = String.Concat("".PadLeft(2, output.Formatter.IndentChar), "[Exit] Yalf.TestMethod(returnVal) duration 345ms");

            int threadId = 22;
            var threadData = new ThreadData(threadId, "cotton", null);
            output.Initialise();
            output.HandleThread(threadData);

            // Act
            output.HandleMethodExit(entry, 1, true);
            output.Complete();

            // Assert
            var outputText = output.GetThreadEntries();
            Assert.That(outputText.Count, Is.EqualTo(1), "Expected one thread collection");
            Assert.That(outputText[threadId].Count, Is.EqualTo(1), "Expected the thread to have one log entry");
            Assert.That(outputText[threadId][0], Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void HandleMethodExit_LogNotEnabled_NoTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new ThreadCollectionOutputHandler(filters);
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            output.Initialise();

            int threadId = 22;
            var threadData = new ThreadData(threadId, "cotton", null);
            output.Initialise();
            output.HandleThread(threadData);

            // Act
            output.HandleMethodExit(entry, 1, false);
            output.Complete();

            // Assert
            var outputText = output.GetThreadEntries();
            Assert.That(outputText.Count, Is.EqualTo(1), "Expected one thread collection");
            Assert.That(outputText[threadId].Count, Is.EqualTo(0), "Expected the thread to have no log entries");
        }
 public void HandleMethodExit(MethodExit entry, int indentLevel, bool displayEnabled)
 {
     _outputHandler.HandleMethodExit(entry, indentLevel, displayEnabled);
 }
        public void FormatThread_ValidLog_NullIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();

            var entry1 = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            var entry2 = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var entry = new ThreadData(22, String.Empty, new BaseEntry[] { entry1, entry2 });

            // Act
            var outputText = formatter.FormatThread(entry, filters);

            // Assert
            Assert.That(outputText, Is.Null, "Did not expect any formatted text back for a delimited values formatter, threadid is in each row.");
        }
        public void FormatMethodExit_HideMethodReturnValueIsSet_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = LogFiltersBuilder.Create().WithMethodReturnValueHidden().Build();
            var formatter = new DefaultFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = "[Exit] Yalf.TestMethod() duration 345ms";

            // Act
            var outputText = formatter.FormatMethodExit(22, 1, 33, entry, filters, true);

            // Assert
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatMethodExit_ReturnRecordedIsFalse_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new SingleLineFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, false, "returnVal");
            var expectedText = "Yalf.TestMethod() started 22:22:31.678 duration 345ms";

            var relatedEntry = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act
            var orderedOutput = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);

            // Assert
            Assert.That(orderedOutput.Count, Is.EqualTo(1), "Expected one string to be returned");
            Assert.That(orderedOutput[0].Level, Is.EqualTo(0), "Not the expected nested level, you may need to adjust the test if the formatter has been changed.");
            Assert.That(orderedOutput[0].FormattedLine, Is.EqualTo(expectedText), "Not the expected formatted line, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatThread_ValidLogWithThreadName_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DefaultFormatter();

            var entry1 = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            var entry2 = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var entry = new ThreadData(22, "YalfThread", new BaseEntry[] { entry1, entry2 });

            var expectedText = "[Thread 22 'YalfThread']";

            // Act
            var outputText = formatter.FormatThread(entry, filters);

            // Assert
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatMethodExit_CallMade_ExceptionIsThrown()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new SingleLineFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            // Act, Assert
            var outputText = formatter.FormatMethodExit(1, 2, 33, entry, filters, true);
        }
        public void HandleMethodExit_LogNotEnabled_NoTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new DefaultOutputHandler(filters);
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            output.Initialise();

            // Act
            output.HandleMethodExit(entry, 1, false);
            output.Complete();

            // Assert
            var outputText = output.GetReport();
            Assert.That(outputText, Is.Empty, "Expected no text in the report output");
        }
        public void FormatMethodExit_HideMethodDurationIsSet_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = LogFiltersBuilder.Create().WithMethodDurationHidden().Build();
            var formatter = new SingleLineFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var expectedText = "Yalf.TestMethod(returnVal) started 22:22:31.678";

            var relatedEntry = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            formatter.FormatMethodEntry(1, 2, 33, relatedEntry, filters, true);

            // Act
            var orderedOutput = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);

            // Assert
            Assert.That(orderedOutput.Count, Is.EqualTo(1), "Expected one string to be returned");
            Assert.That(orderedOutput[0].Level, Is.EqualTo(0), "Not the expected nested level, you may need to adjust the test if the formatter has been changed.");
            Assert.That(orderedOutput[0].FormattedLine, Is.EqualTo(expectedText), "Not the expected formatted line, you may need to adjust the test if the formatter has been changed.");
        }
        public void HandleThread_ValidLogWithThreadName_ExpectedTextIsReturned()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var output = new DefaultOutputHandler(filters);

            var entry1 = new MethodEntry(1, "Yalf.TestMethod", new[] { "param1", "param2" }, DateTime.Parse("2022-10-22 22:22:31.678"));
            var entry2 = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");
            var entry = new ThreadData(22, "YalfThread", new BaseEntry[] { entry1, entry2 });

            var expectedText = String.Concat("[Thread 22 'YalfThread']");
            output.Initialise();

            // Act
            output.HandleThread(entry);
            output.Complete();

            // Assert
            var outputText = output.GetReport();
            Assert.That(outputText, Is.Not.Empty, "Expected a string to be returned");
            Assert.That(output.CurrentThreadId, Is.EqualTo(22), "Not the expected thread id for the current thread.");
            Assert.That(outputText, Is.EqualTo(expectedText), "Not the expected output text, you may need to adjust the test if the formatter has been changed.");
        }
        public void FormatMethodExit_SuppliedEntryMethodNameDoesNotMatch_ExceptionIsThrown()
        {
            // Arrange
            var filters = this.GetDefaultFilters();
            var formatter = new DelimitedValuesFormatter();
            var entry = new MethodExit(1, "Yalf.TestMethod", 345, true, "returnVal");

            // Act, Assert
            var outputText = formatter.FormatMethodExitForSingleLineOutput(1, 2, 33, entry, filters, true);
        }