Пример #1
0
        public static IDisposable Push([NotNull] ElmScope scope, [NotNull] ElmStore store)
        {
            var temp = Current;

            Current        = scope;
            Current.Parent = temp;

            Current.Node = new ScopeNode()
            {
                StartTime = DateTimeOffset.UtcNow,
                State     = Current._state,
                Name      = Current._name
            };

            if (Current.Parent != null)
            {
                Current.Node.Parent = Current.Parent.Node;
                Current.Parent.Node.Children.Add(Current.Node);
            }
            else
            {
                Current.Context.Root = Current.Node;
                store.AddActivity(Current.Context);
            }

            return(new DisposableAction(() =>
            {
                Current.Node.EndTime = DateTimeOffset.UtcNow;
                Current = Current.Parent;
            }));
        }
Пример #2
0
        public async void Invoke_WithNonMatchingPath_IgnoresRequest()
        {
            // Arrange
            var elmStore = new ElmStore();
            var factory = new LoggerFactory();
            var optionsMock = new Mock<IOptions<ElmOptions>>();
            optionsMock
                .SetupGet(o => o.Options)
                .Returns(new ElmOptions());
            factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object.Options));

            RequestDelegate next = _ =>
            {
                return Task.FromResult<object>(null);
            };

            var captureMiddleware = new ElmCaptureMiddleware(
                next,
                factory,
                optionsMock.Object);
            var pageMiddleware = new ElmPageMiddleware(
                next,
                optionsMock.Object,
                elmStore);

            var contextMock = GetMockContext("/nonmatchingpath");

            // Act
            await captureMiddleware.Invoke(contextMock.Object);
            await pageMiddleware.Invoke(contextMock.Object);

            // Assert
            // Request.Query is used by the ElmPageMiddleware to parse the query parameters
            contextMock.VerifyGet(c => c.Request.Query, Times.Never());
        }
Пример #3
0
        private Tuple<ElmLogger, ElmStore> SetUp(Func<string, LogLevel, bool> filter = null, string name = null)
        {
            // Arrange
            var store = new ElmStore();
            var options = new ElmOptions() { Filter = filter ?? _filter };
            var provider = new ElmLoggerProvider(store, options);
            var logger = (ElmLogger)provider.CreateLogger(name ?? _name);

            return new Tuple<ElmLogger, ElmStore>(logger, store);
        }
Пример #4
0
        public ElmLoggerProvider(ElmStore store, ElmOptions options)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _store   = store;
            _options = options;
        }
Пример #5
0
        public ElmLoggerProvider(ElmStore store, ElmOptions options)
        {
            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _store = store;
            _options = options;
        }
Пример #6
0
        public async void Invoke_WithMatchingPath_FulfillsRequest()
        {
            // Arrange
            var elmStore = new ElmStore();
            var factory = new LoggerFactory();
            var optionsMock = new Mock<IOptions<ElmOptions>>();
            optionsMock
                .SetupGet(o => o.Options)
                .Returns(new ElmOptions());
            factory.AddProvider(new ElmLoggerProvider(elmStore, optionsMock.Object.Options));

            RequestDelegate next = _ =>
            {
                return Task.FromResult<object>(null);
            };

            var captureMiddleware = new ElmCaptureMiddleware(
                next,
                factory,
                optionsMock.Object);
            var pageMiddleware = new ElmPageMiddleware(
                next,
                optionsMock.Object,
                elmStore);
            var contextMock = GetMockContext("/Elm");

            using (var responseStream = new MemoryStream())
            {
                contextMock
                    .SetupGet(c => c.Response.Body)
                    .Returns(responseStream);
                contextMock
                    .SetupGet(c => c.ApplicationServices)
                    .Returns(() => null);

                // Act
                await captureMiddleware.Invoke(contextMock.Object);
                await pageMiddleware.Invoke(contextMock.Object);

                string response = Encoding.UTF8.GetString(responseStream.ToArray());

                // Assert
                contextMock.VerifyGet(c => c.Request.Query, Times.AtLeastOnce());
                Assert.True(response.Contains("<title>ASP.NET Logs</title>"));
            }
        }
        private IEnumerable<ActivityContextDto> GetLogDetails(ElmStore elmStore)
        {
            var activities = new List<ActivityContextDto>();
            foreach (var activity in elmStore.GetActivities().Reverse())
            {
                var rootScopeNodeDto = new ScopeNodeDto();
                CopyScopeNodeTree(activity.Root, rootScopeNodeDto);

                activities.Add(new ActivityContextDto()
                {
                    RequestInfo = GetRequestInfoDto(activity.HttpInfo),
                    Id = activity.Id,
                    RepresentsScope = activity.RepresentsScope,
                    Root = rootScopeNodeDto
                });
            }

            return activities;
        }
        public Task Invoke(HttpContext context, ElmStore elmStore)
        {
            var currentRequest = context.Request;

            var logActivities = GetLogDetails(elmStore);

            context.Response.StatusCode = StatusCodes.Status200OK;
            context.Response.ContentType = "application/json";

            var serializer = JsonSerializer.Create();
            using (var writer = new JsonTextWriter(new StreamWriter(stream: context.Response.Body,
                                                                    encoding: Encoding.UTF8,
                                                                    bufferSize: 1024,
                                                                    leaveOpen: true)))
            {
                serializer.Serialize(writer, logActivities);
            }

            return Task.FromResult(true);
        }
Пример #9
0
        public static IDisposable Push(ElmScope scope, ElmStore store)
        {
            if (scope == null)
            {
                throw new ArgumentNullException(nameof(scope));
            }

            if (store == null)
            {
                throw new ArgumentNullException(nameof(store));
            }

            var temp = Current;
            Current = scope;
            Current.Parent = temp;

            Current.Node = new ScopeNode()
            {
                StartTime = DateTimeOffset.UtcNow,
                State = Current._state,
                Name = Current._name
            };

            if (Current.Parent != null)
            {
                Current.Node.Parent = Current.Parent.Node;
                Current.Parent.Node.Children.Add(Current.Node);
            }
            else
            {
                Current.Context.Root = Current.Node;
                store.AddActivity(Current.Context);
            }

            return new DisposableAction(() =>
            {
                Current.Node.EndTime = DateTimeOffset.UtcNow;
                Current = Current.Parent;
            });
        }
Пример #10
0
 public ElmPageMiddleware(RequestDelegate next, IOptions <ElmOptions> options, ElmStore store)
 {
     _next    = next;
     _options = options.Value;
     _store   = store;
 }
Пример #11
0
 public ElmPageMiddleware(RequestDelegate next, IOptions<ElmOptions> options, ElmStore store)
 {
     _next = next;
     _options = options.Value;
     _store = store;
 }
Пример #12
0
 public ElmLogger(string name, ElmOptions options, ElmStore store)
 {
     _name    = name;
     _options = options;
     _store   = store;
 }
Пример #13
0
 public ElmLogger(string name, ElmOptions options, ElmStore store)
 {
     _name = name;
     _options = options;
     _store = store;
 }
Пример #14
0
 public ElmLoggerProvider([NotNull] ElmStore store, [NotNull] ElmOptions options)
 {
     _store   = store;
     _options = options;
 }
 public ElmLoggerProvider([NotNull] ElmStore store, [NotNull] ElmOptions options)
 {
     _store = store;
     _options = options;
 }