Ejemplo n.º 1
0
        public void LogWrite___Should_create_LogItem___When_logging_real_object_subject_via_lambda_with_comment()
        {
            // Arrange
            var subject = new TestObjectWithToString();
            var comment = "this is a comment " + A.Dummy <string>();

            // Act
            Log.Write(() => subject, comment);

            // Assert
            var logItem       = this.loggedItems.Single();
            var actualSubject = logItem.Subject.DeserializeSubject <TestObjectWithToString>();

            logItem.Subject.Summary.Should().Be(subject.ToString());
            actualSubject.ToString().Should().Be(subject.ToString());
            logItem.Kind.Should().Be(LogItemKind.Object);
            logItem.Comment.Should().Be(comment);

            logItem.Context.Should().NotBeNull();
            logItem.Context.StackTrace.Should().BeNull();
            logItem.Context.Origin.Should().Be(this.expectedOrigin);
            logItem.Context.CallingMethod.Should().NotBeNullOrWhiteSpace();
            logItem.Context.CallingType.Should().NotBeNull();
            logItem.Context.MachineName.Should().NotBeNullOrWhiteSpace();
            logItem.Context.ProcessFileVersion.Should().NotBeNullOrWhiteSpace();
            logItem.Context.ProcessName.Should().NotBeNullOrWhiteSpace();
            logItem.Context.TimestampUtc.Should().BeOnOrBefore(DateTime.UtcNow);

            logItem.Correlations.Should().BeEmpty();
        }
Ejemplo n.º 2
0
        public void Log_Enter_Activity_Trace___Records_details_correctly___With_exception_and_comment()
        {
            // Arrange
            var enterSubject          = new TestObjectWithToString();
            var stringTraceWithLambda = "some trace with a lambda" + A.Dummy <string>();
            var traceObjectWithLambda = new DifferentTestObjectWithToString();
            var exceptionToThrow      = new InvalidOperationException("Oh no.");

            // Act
            using (var log = Log.With(() => enterSubject))
            {
                // to make sure the next message doesn't come too quickly for the assert later.
                try
                {
                    throw exceptionToThrow;
                }
                catch (Exception exception)
                {
                    log.Write(() => exception);
                }

                log.Write(() => stringTraceWithLambda);
                log.Write(() => traceObjectWithLambda);
            }

            // Assert
            this.loggedItems.ToList().ForEach(_ => _.Correlations.Single(c => c is ElapsedCorrelation).Should().NotBeNull());
            this.loggedItems.ToList().ForEach(_ => _.Correlations.Single(c => c is SubjectCorrelation).Should().NotBeNull());
            this.loggedItems.Select(_ => _.Correlations.Single(c => c is ElapsedCorrelation)).Count().Should()
            .Be(this.loggedItems.Count);
            this.loggedItems.Select(_ => _.Correlations.Single(c => c is SubjectCorrelation)).Count().Should()
            .Be(this.loggedItems.Count);
            this.loggedItems.ToList().ForEach(
                _ => ((SubjectCorrelation)_.Correlations.Single(c => c is SubjectCorrelation)).Subject
                .DeserializeSubject <TestObjectWithToString>().Test.Should().Be(enterSubject.Test));

            var enterItem = this.loggedItems.Single(_ => _.Correlations.Any(c => c is OrderCorrelation oc && oc.Position == 0));

            enterItem.Subject.DeserializeSubject <string>().Should().Be(UsingBlockLogger.InitialItemOfUsingBlockSubject);
            var enterCorrelation = (ElapsedCorrelation)enterItem.Correlations.Single(_ => _ is ElapsedCorrelation);

            enterCorrelation.ElapsedTime.TotalMilliseconds.Should().Be(0);

            var middleItems = this.loggedItems.Where(
                _ => new[] { exceptionToThrow.Message, stringTraceWithLambda, traceObjectWithLambda.ToString() }.Any(
                    a => _.Subject.Summary.EndsWith(a, StringComparison.CurrentCulture))).ToList();

            middleItems.ForEach(_ =>
            {
                _.Correlations.Any(c => c is OrderCorrelation oc && oc.Position > 0 && oc.Position < 4).Should().BeTrue();
                var middleCorrelation = (ElapsedCorrelation)_.Correlations.Single(s => s is ElapsedCorrelation);
                middleCorrelation.ElapsedTime.TotalMilliseconds.Should().BeGreaterThan(0);
            });

            var exitItem = this.loggedItems.Single(_ => _.Correlations.Any(c => c is OrderCorrelation oc && oc.Position == 4));

            exitItem.Subject.DeserializeSubject <string>().Should().Be(UsingBlockLogger.FinalItemOfUsingBlockSubject);
            var exitCorrelation = (ElapsedCorrelation)exitItem.Correlations.Single(_ => _ is ElapsedCorrelation);

            exitCorrelation.ElapsedTime.TotalMilliseconds.Should().BeGreaterThan(0);
        }