Пример #1
0
        /// <summary>
        /// Logs query data to a data store
        /// </summary>
        /// <param name="queries">The <see cref="Query"/> object or objects to be logged</param>
        public void Log(params Query[] queries)
        {
            if (!_settings.Value.IsEnabled)
            {
                return;
            }

            foreach (var query in queries)
            {
                var host      = _context.Host;
                var ipAddress = (query.IPAddress.IsNullOrWhiteSpace())
                    ? _context.IPAddress : query.IPAddress;

                query.QueryID         = (query.QueryID is null) ? Guid.NewGuid() : query.QueryID;
                query.ApplicationName = (query.ApplicationName.IsNullOrWhiteSpace())
                    ? _settings.Value.ApplicationName : query.ApplicationName;
                query.QueryTerms = query.QueryTerms.Truncate(_settings.Value.MaxQueryTermsLength);
                query.QueryText  = query.QueryText.Truncate(_settings.Value.MaxQueryTextLength);
                query.Host       = (query.Host.IsNullOrWhiteSpace()) ? host : query.Host;
                query.IPAddress  = IPAddressProcessor.Process(ipAddress, _settings.Value);
                query.LogDate    = (query.LogDate is null) ? DateTime.UtcNow : query.LogDate;
            }

            _store.Enqueue(queries);
        }
Пример #2
0
        public void Enqueue()
        {
            var queue = new List <Query>();

            // Add single query
            var query = new Query()
            {
                QueryTerms = "test"
            };

            _store.Enqueue(new[] { query });

            if (_store.LogQueue.Reader.TryRead(out Query singleQuery))
            {
                queue.Add(singleQuery);
            }

            Assert.Single(queue);
            Assert.Equal("test", queue[0].QueryTerms);

            // Add multiple queries
            queue = new List <Query>();

            var query1 = new Query()
            {
                QueryTerms = "test1"
            };

            var query2 = new Query()
            {
                QueryTerms = "test2"
            };

            var query3 = new Query()
            {
                QueryTerms = "test3",
            };

            // Test queue size
            _store.Enqueue(new[] { query1, query2, query3 });

            if (_store.LogQueue.Reader.TryRead(out Query q1))
            {
                queue.Add(q1);
            }

            if (_store.LogQueue.Reader.TryRead(out Query q2))
            {
                queue.Add(q2);
            }

            if (_store.LogQueue.Reader.TryRead(out Query q3))
            {
                queue.Add(q3);
            }

            Assert.Equal(3, queue.Count);
            Assert.Equal("test1", queue[0].QueryTerms);
            Assert.Equal("test2", queue[1].QueryTerms);
            Assert.Equal("test3", queue[2].QueryTerms);
        }