Exemplo n.º 1
0
        public void Construct_StaticTrace()
        {
            UseStaticTraceApi();
            ExpectTraceOperation("a");

            using (var op = new TraceOperation("a")) { }
        }
Exemplo n.º 2
0
        public void Do_FuncWithTraceSource()
        {
            ExpectTraceOperation("Test");

            var result = TraceOperation.Do(Trace, "Test", () => 42);

            result.Should().Be(42);
        }
Exemplo n.º 3
0
        public async Task DoAsync_FuncWithTraceSource()
        {
            ExpectTraceOperation("Test");

            var result = await TraceOperation.DoAsync(Trace, "Test", () => Task.FromResult(42));

            result.Should().Be(42);
        }
Exemplo n.º 4
0
        public void Do_FuncWithStaticTrace()
        {
            UseStaticTraceApi();
            ExpectTraceOperation("Test");

            var result = TraceOperation.Do("Test", () => 42);

            result.Should().Be(42);
        }
Exemplo n.º 5
0
        public void Name_Explicit()
        {
            ExpectTraceOperation("Test");

            using (var op = new TraceOperation(Trace, "Test"))
            {
                op.Name.Should().Be("Test");
            }
        }
Exemplo n.º 6
0
        public async Task DoAsync_FuncWithStaticTrace()
        {
            UseStaticTraceApi();
            ExpectTraceOperation("Test");

            var result = await TraceOperation.DoAsync("Test", () => Task.FromResult(42));

            result.Should().Be(42);
        }
Exemplo n.º 7
0
        public void Exception_Default()
        {
            ExpectTraceOperation("Test");

            using (var op = new TraceOperation(Trace, "Test"))
            {
                op.Exception.Should().BeNull();
            }
        }
Exemplo n.º 8
0
        public void Name_Null()
        {
            // No expected tracing

            using (var op = new TraceOperation(Trace, null))
            {
                op.Name.Should().BeNull();
            }
        }
Exemplo n.º 9
0
        public void Name_Default()
        {
            ExpectTraceOperation(nameof(Name_Default));

            using (var op = new TraceOperation(Trace))
            {
                op.Name.Should().Be(nameof(Name_Default));
            }
        }
Exemplo n.º 10
0
        public async Task DoAsync_ActionWithTraceSource()
        {
            var count = 0;

            ExpectTraceOperation("Test");

            await TraceOperation.DoAsync(Trace, "Test", () => { count++; return(Task.CompletedTask); });

            count.Should().Be(1);
        }
Exemplo n.º 11
0
        public void Do_ActionWithTraceSource()
        {
            var count = 0;

            ExpectTraceOperation("Test");

            TraceOperation.Do(Trace, "Test", () => { count++; });

            count.Should().Be(1);
        }
Exemplo n.º 12
0
        public void Do_FuncWithTraceSource_Exception()
        {
            var e = CreateTestException();

            ExpectTraceOperation("Test");
            ExpectTraceData(Error, 0, e);

            this.Invoking(_ => TraceOperation.Do(Trace, "Test", () => null as int? ?? throw e))
            .Should().Throw <ApplicationException>();
        }
Exemplo n.º 13
0
        public void DoAsync_FuncWithTraceSource_Exception()
        {
            var e = CreateTestException();

            ExpectTraceOperation("Test");
            ExpectTraceData(Error, 0, e);

            this.Awaiting(_ => TraceOperation.DoAsync(Trace, "Test", () => Task.FromResult(null as int? ?? throw e)))
            .Should().Throw <ApplicationException>();
        }
Exemplo n.º 14
0
        public void DoAsync_ActionWithTraceSource_Exception()
        {
            var e = CreateTestException();

            ExpectTraceOperation("Test");
            ExpectTraceData(Error, 0, e);

            this.Awaiting(_ => TraceOperation.DoAsync(Trace, "Test", () => { throw e; }))
            .Should().Throw <ApplicationException>();
        }
Exemplo n.º 15
0
        public void DoAsync_ActionWithStaticTrace_Exception()
        {
            var e = CreateTestException();

            UseStaticTraceApi();
            ExpectTraceOperation("Test");
            ExpectTraceEvent(Error, 0, s => s.Contains(e.Message));

            this.Awaiting(_ => TraceOperation.DoAsync("Test", () => { throw e; }))
            .Should().Throw <ApplicationException>();
        }
Exemplo n.º 16
0
        public void Do_ActionWithStaticTrace()
        {
            var count = 0;

            UseStaticTraceApi();
            ExpectTraceOperation("Test");

            TraceOperation.Do("Test", () => { count++; });

            count.Should().Be(1);
        }
Exemplo n.º 17
0
        public async Task DoAsync_ActionWithStaticTrace()
        {
            var count = 0;

            UseStaticTraceApi();
            ExpectTraceOperation("Test");

            await TraceOperation.DoAsync("Test", () => { count++; return(Task.CompletedTask); });

            count.Should().Be(1);
        }
Exemplo n.º 18
0
        public void DoAsync_FuncWithStaticTrace_Exception()
        {
            var e = CreateTestException();

            UseStaticTraceApi();
            ExpectTraceOperation("Test");
            ExpectTraceEvent(Error, 0, s => s.Contains(e.Message));

            this.Awaiting(_ => TraceOperation.DoAsync("Test", () => Task.FromResult(null as int? ?? throw e)))
            .Should().Throw <ApplicationException>();
        }
Exemplo n.º 19
0
        public void Exception_Explicit()
        {
            var e = new ApplicationException("Something bad happened.");

            ExpectTraceOperation("Test");
            ExpectTraceData(Error, 0, e);

            using (var op = new TraceOperation(Trace, "Test"))
            {
                op.Exception = e;
                op.Exception.Should().BeSameAs(e);
            }
        }
Exemplo n.º 20
0
        public void StartTime()
        {
            ExpectTraceOperation("Test");

            var min = GetUtcNow();

            using (var op = new TraceOperation(Trace, "Test"))
            {
                var max = GetUtcNow();

                var value = op.StartTime;

                value.Should().BeOnOrAfter(min);
                value.Should().BeOnOrBefore(max);
            }
        }
Exemplo n.º 21
0
        public static async Task <TResult> DoAsync <TResult>(TraceSource?trace, string?name, Func <Task <TResult> > action)
        {
            if (action is null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using var operation = new TraceOperation(trace, name);

            try
            {
                return(await action());
            }
            catch (Exception e)
            {
                operation.Exception = e;
                throw;
            }
        }
Exemplo n.º 22
0
        public static void Do(TraceSource?trace, string?name, Action action)
        {
            if (action is null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            using var operation = new TraceOperation(trace, name);

            try
            {
                action();
            }
            catch (Exception e)
            {
                operation.Exception = e;
                throw;
            }
        }
Exemplo n.º 23
0
        public void Elapsed()
        {
            ExpectTraceOperation("Test");

            var before = GetUtcNow();

            using (var op = new TraceOperation(Trace, "Test"))
            {
                var after = GetUtcNow();

                Thread.Sleep(20.Milliseconds());

                var min   = GetUtcNow() - after;
                var value = op.ElapsedTime;
                var max   = GetUtcNow() - before;

                value.Should().BeGreaterOrEqualTo(min);
                value.Should().BeLessOrEqualTo(max);
            }
        }
Exemplo n.º 24
0
        [NonParallelizable] // becuase it depends on static state
        public void CorrelationIds_Hierarchical()
        {
            ExpectTraceOperation("Test");

            var priorFormat = Activity.DefaultIdFormat;
            var priorForced = Activity.ForceDefaultIdFormat;

            try
            {
                Activity.DefaultIdFormat      = ActivityIdFormat.Hierarchical;
                Activity.ForceDefaultIdFormat = false;

                Activity.Current.Should().BeNull();
                LegacyActivityId.Should().BeEmpty();
                LegacyOperationStack.Should().BeEmpty();

                using (var op = new TraceOperation(Trace, "Test"))
                {
                    Activity.Current.Should().NotBeNull();
                    Activity.Current.OperationName.Should().Be(op.Name);
                    Activity.Current.IdFormat.Should().Be(ActivityIdFormat.Hierarchical);

                    LegacyActivityId.Should().NotBeEmpty();
                    LegacyOperationStack.Should().Equal(new[] { Activity.Current.Id });
                }

                Activity.Current.Should().BeNull();
                LegacyActivityId.Should().BeEmpty();
                LegacyOperationStack.Should().BeEmpty();
            }
            finally
            {
                Activity.DefaultIdFormat      = priorFormat;
                Activity.ForceDefaultIdFormat = priorForced;
            }
        }
Exemplo n.º 25
0
 public void DoAsync_ActionWithStaticTrace_NullAction()
 {
     Awaiting(() => TraceOperation.DoAsync("Test", null !))
     .Should().Throw <ArgumentNullException>();
 }
Exemplo n.º 26
0
 public void Do_ActionWithTraceSource_NullAction()
 {
     Invoking(() => TraceOperation.Do(Trace, "Test", null !))
     .Should().Throw <ArgumentNullException>();
 }
Exemplo n.º 27
0
 private static DateTime GetUtcNow()
 => TraceOperation.GetUtcNow();
Exemplo n.º 28
0
 public void Do_FuncWithStaticTrace_NullAction()
 {
     Invoking(() => TraceOperation.Do <object>("Test", null !))
     .Should().Throw <ArgumentNullException>();
 }
Exemplo n.º 29
0
 public void DoAsync_FuncWithTraceSource_NullAction()
 {
     Awaiting(() => TraceOperation.DoAsync <object>(Trace, "Test", null !))
     .Should().Throw <ArgumentNullException>();
 }
Exemplo n.º 30
0
        public void Construct_TraceSource()
        {
            ExpectTraceOperation("a");

            using (var op = new TraceOperation(Trace, "a")) { }
        }