diff --git a/src/WireMock.Net/Owin/WireMockMiddleware.cs b/src/WireMock.Net/Owin/WireMockMiddleware.cs index 2e9cd3d0f..d2bbec9b1 100644 --- a/src/WireMock.Net/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net/Owin/WireMockMiddleware.cs @@ -162,10 +162,9 @@ private void LogRequest(LogEntry entry, bool addRequest) if (_options.MaxRequestLogCount != null) { - var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value; - for (int i = 0; i < amount; i++) + foreach (var logEntry in _options.LogEntries.OrderBy(le => le.RequestMessage.DateTime).Take(_options.LogEntries.Count - _options.MaxRequestLogCount.Value).ToList()) { - _options.LogEntries.RemoveAt(0); + _options.LogEntries.Remove(logEntry); } } @@ -173,13 +172,9 @@ private void LogRequest(LogEntry entry, bool addRequest) { var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value); - for (var i = _options.LogEntries.Count - 1; i >= 0; i--) + foreach (var logEntry in _options.LogEntries.Where(le => le.RequestMessage.DateTime < checkTime).ToList()) { - var le = _options.LogEntries[i]; - if (le.RequestMessage.DateTime <= checkTime) - { - _options.LogEntries.RemoveAt(i); - } + _options.LogEntries.Remove(logEntry); } } }