private void LogRequest(LogEntry entry, bool addRequest)
        {
            _options.Logger.DebugRequestResponse(LogEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));

            if (addRequest)
            {
                _options.LogEntries.Add(entry);
            }

            if (_options.MaxRequestLogCount != null)
            {
                var logEntries = _options.LogEntries.ToList();
                foreach (var logEntry in logEntries.OrderBy(le => le.RequestMessage.DateTime).Take(logEntries.Count - _options.MaxRequestLogCount.Value))
                {
                    _options.LogEntries.Remove(logEntry);
                }
            }

            if (_options.RequestLogExpirationDuration != null)
            {
                var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);

                foreach (var logEntry in _options.LogEntries.ToList().Where(le => le.RequestMessage.DateTime < checkTime))
                {
                    _options.LogEntries.Remove(logEntry);
                }
            }
        }
Example #2
0
        private void LogRequest(LogEntry entry, bool addRequest)
        {
            _options.Logger.DebugRequestResponse(LogEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));

            if (addRequest)
            {
                _options.LogEntries.Add(entry);
            }

            if (_options.MaxRequestLogCount != null)
            {
                var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
                for (int i = 0; i < amount; i++)
                {
                    _options.LogEntries.RemoveAt(0);
                }
            }

            if (_options.RequestLogExpirationDuration != null)
            {
                var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);

                for (var i = _options.LogEntries.Count - 1; i >= 0; i--)
                {
                    var le = _options.LogEntries[i];
                    if (le.RequestMessage.DateTime <= checkTime)
                    {
                        _options.LogEntries.RemoveAt(i);
                    }
                }
            }
        }
        public void LogEntryMapper_Map_LogEntry_Check_ResponseBodyTypeFile()
        {
            // Assign
            var logEntry = new LogEntry
            {
                RequestMessage = new RequestMessage(new UrlDetails("http://localhost"), "get", "::1"
                                                    ),
                ResponseMessage = new ResponseMessage
                {
                    BodyData = new BodyData
                    {
                        DetectedBodyType = BodyType.File,
                        BodyAsFile       = "test"
                    }
                }
            };

            // Act
            var result = LogEntryMapper.Map(logEntry);

            // Assert
            Check.That(result.Request.DetectedBodyType).IsNull();
            Check.That(result.Request.DetectedBodyTypeFromContentType).IsNull();
            Check.That(result.Request.BodyAsBytes).IsNull();
            Check.That(result.Request.Body).IsNull();
            Check.That(result.Request.BodyAsJson).IsNull();

            Check.That(result.Response.DetectedBodyType).IsEqualTo(BodyType.File);
            Check.That(result.Response.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.None);
            Check.That(result.Request.BodyAsBytes).IsNull();
            Check.That(result.Response.Body).IsNull();
            Check.That(result.Response.BodyAsJson).IsNull();
            Check.That(result.Response.BodyAsFile).IsEqualTo("test");
        }
        public void LogEntryMapper_Map_LogEntry_WithFault()
        {
            // Assign
            var logEntry = new LogEntry
            {
                RequestMessage  = new RequestMessage(new UrlDetails("http://localhost"), "get", "::1"),
                ResponseMessage = new ResponseMessage
                {
                    BodyData = new BodyData
                    {
                        DetectedBodyType = BodyType.File,
                        BodyAsFile       = "test"
                    },
                    FaultType       = FaultType.EMPTY_RESPONSE,
                    FaultPercentage = 0.5
                }
            };

            // Act
            var result = LogEntryMapper.Map(logEntry);

            // Assert
            result.Response.FaultType.Should().Be("EMPTY_RESPONSE");
            result.Response.FaultPercentage.Should().Be(0.5);
        }
Example #5
0
        public async Task <IActionResult> CreateAsync([FromBody] InLogEntryDTO entryDTO)
        {
            var    userId   = HttpContext.User.GetUserId();
            double?overtime = null;

            if (!userId.HasValue)
            {
                return(Unauthorized());
            }

            if (entryDTO.Type == DayType.WorkDay)
            {
                if (!entryDTO.EndTime.HasValue || entryDTO.StartTime > entryDTO.EndTime)
                {
                    return(BadRequest());
                }

                overtime = (DateTimeOffset.FromUnixTimeSeconds(entryDTO.EndTime.Value).UtcDateTime
                            - DateTimeOffset.FromUnixTimeSeconds(entryDTO.StartTime).UtcDateTime
                            - TimeSpan.FromHours(8)).TotalHours;

                overtime = overtime < 0 ? null : overtime;
            }
            else if (entryDTO.Type == DayType.DayOff)
            {
                if (!entryDTO.VacationId.HasValue)
                {
                    return(BadRequest());
                }

                var vacation = await _repositoryManager.VacationRepository
                               .GetAsync(userId.Value, entryDTO.VacationId.Value);

                if (vacation == null)
                {
                    return(NotFound());
                }

                if (!vacation.Active || vacation.MaximumDays == 0 ||
                    vacation.UsedDays >= vacation.MaximumDays)
                {
                    return(BadRequest());
                }

                vacation.UsedDays += 1;
            }

            var entry = LogEntryMapper.Map(userId.Value, entryDTO, overtime);

            entry = _repositoryManager.LogEntryRepository.Create(entry);

            await _repositoryManager.SaveAsync();

            return(Ok(LogEntryMapper.Map(entry)));
        }
Example #6
0
        public async Task <IActionResult> GetAsync([FromRoute] long from,
                                                   [FromRoute] long to, [FromRoute] int id, [FromQuery] bool all)
        {
            var start = DateTimeOffset.FromUnixTimeSeconds(from).UtcDateTime;
            var end   = DateTimeOffset.FromUnixTimeSeconds(to).UtcDateTime;

            var entries = await _repositoryManager.LogEntryRepository
                          .GetAsync(id, start, end, false, all);

            return(Ok(entries.Select(e => LogEntryMapper.Map(e))));
        }
        private ResponseMessage RequestGet(RequestMessage requestMessage)
        {
            Guid guid  = Guid.Parse(requestMessage.Path.Substring(AdminRequests.Length + 1));
            var  entry = LogEntries.FirstOrDefault(r => !r.RequestMessage.Path.StartsWith("/__admin/") && r.Guid == guid);

            if (entry == null)
            {
                _logger.Warn("HttpStatusCode set to 404 : Request not found");
                return(ResponseMessageBuilder.Create("Request not found", 404));
            }

            var model = LogEntryMapper.Map(entry);

            return(ToJson(model));
        }
Example #8
0
        public async Task <IActionResult> GetAsync([FromRoute] long from, [FromRoute] long to)
        {
            var userId = HttpContext.User.GetUserId();

            if (!userId.HasValue)
            {
                return(Unauthorized());
            }

            var start = DateTimeOffset.FromUnixTimeSeconds(from).UtcDateTime;
            var end   = DateTimeOffset.FromUnixTimeSeconds(to).UtcDateTime;

            var entries = await _repositoryManager.LogEntryRepository
                          .GetAsync(userId.Value, start, end);

            return(Ok(entries.Select(e => LogEntryMapper.Map(e))));
        }
        public void LogEntryMapper_Map_LogEntry_Check_BodyTypeBytes()
        {
            // Assign
            var logEntry = new LogEntry
            {
                RequestMessage = new RequestMessage(
                    new UrlDetails("http://localhost"),
                    "post",
                    "::1",
                    new BodyData
                {
                    DetectedBodyType = BodyType.Bytes,
                    BodyAsBytes      = new byte[] { 0 }
                }
                    ),
                ResponseMessage = new ResponseMessage
                {
                    BodyData = new BodyData
                    {
                        DetectedBodyType = BodyType.Bytes,
                        BodyAsBytes      = new byte[] { 0 }
                    }
                }
            };

            // Act
            var result = LogEntryMapper.Map(logEntry);

            // Assert
            Check.That(result.Request.DetectedBodyType).IsEqualTo("Bytes");
            Check.That(result.Request.DetectedBodyTypeFromContentType).IsEqualTo("None");
            Check.That(result.Request.BodyAsBytes).ContainsExactly(new byte[] { 0 });
            Check.That(result.Request.Body).IsNull();
            Check.That(result.Request.BodyAsJson).IsNull();

            Check.That(result.Response.DetectedBodyType).IsEqualTo(BodyType.Bytes);
            Check.That(result.Response.DetectedBodyTypeFromContentType).IsEqualTo(BodyType.None);
            Check.That(result.Response.BodyAsBytes).ContainsExactly(new byte[] { 0 });
            Check.That(result.Response.Body).IsNull();
            Check.That(result.Response.BodyAsJson).IsNull();
            Check.That(result.Response.BodyAsFile).IsNull();
        }