mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 15:31:20 +02:00
Use try-catch when adding or removing logEntry (#848)
* Use try-catch when removing logEntry * . * try catch add * Add extra check * ...
This commit is contained in:
@@ -269,29 +269,55 @@ namespace WireMock.Owin
|
|||||||
{
|
{
|
||||||
_options.Logger.DebugRequestResponse(_logEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));
|
_options.Logger.DebugRequestResponse(_logEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));
|
||||||
|
|
||||||
if (addRequest)
|
// If addRequest is set to true and MaxRequestLogCount is null or does have a value greater than 0, try to add a new request log.
|
||||||
|
if (addRequest && _options.MaxRequestLogCount is null or > 0)
|
||||||
{
|
{
|
||||||
_options.LogEntries.Add(entry);
|
TryAddLogEntry(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_options.MaxRequestLogCount != null)
|
// In case MaxRequestLogCount has a value greater than 0, try to delete existing request logs based on the count.
|
||||||
|
if (_options.MaxRequestLogCount is > 0)
|
||||||
{
|
{
|
||||||
var logEntries = _options.LogEntries.ToList();
|
var logEntries = _options.LogEntries.ToList();
|
||||||
foreach (var logEntry in logEntries.OrderBy(le => le.RequestMessage.DateTime).Take(logEntries.Count - _options.MaxRequestLogCount.Value))
|
foreach (var logEntry in logEntries.OrderBy(le => le.RequestMessage.DateTime).Take(logEntries.Count - _options.MaxRequestLogCount.Value))
|
||||||
{
|
{
|
||||||
_options.LogEntries.Remove(logEntry);
|
TryRemoveLogEntry(logEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_options.RequestLogExpirationDuration != null)
|
// In case RequestLogExpirationDuration has a value greater than 0, try to delete existing request logs based on the date.
|
||||||
|
if (_options.RequestLogExpirationDuration is > 0)
|
||||||
{
|
{
|
||||||
var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);
|
var checkTime = DateTime.UtcNow.AddHours(-_options.RequestLogExpirationDuration.Value);
|
||||||
|
|
||||||
foreach (var logEntry in _options.LogEntries.ToList().Where(le => le.RequestMessage.DateTime < checkTime))
|
foreach (var logEntry in _options.LogEntries.ToList().Where(le => le.RequestMessage.DateTime < checkTime))
|
||||||
{
|
{
|
||||||
_options.LogEntries.Remove(logEntry);
|
TryRemoveLogEntry(logEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void TryAddLogEntry(LogEntry logEntry)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_options.LogEntries.Add(logEntry);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Ignore exception (can happen during stress testing)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void TryRemoveLogEntry(LogEntry logEntry)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_options.LogEntries.Remove(logEntry);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// Ignore exception (can happen during stress testing)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ using System.Net;
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
@@ -374,6 +375,26 @@ public class WireMockServerAdminTests
|
|||||||
server.Stop();
|
server.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task WireMockServer_Admin_Logging_SetMaxRequestLogCount_To_0_Should_Not_AddLogging()
|
||||||
|
{
|
||||||
|
// Assign
|
||||||
|
var client = new HttpClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var server = WireMockServer.Start();
|
||||||
|
server.SetMaxRequestLogCount(0);
|
||||||
|
|
||||||
|
await client.GetAsync("http://localhost:" + server.Port + "/foo1").ConfigureAwait(false);
|
||||||
|
await client.GetAsync("http://localhost:" + server.Port + "/foo2").ConfigureAwait(false);
|
||||||
|
await client.GetAsync("http://localhost:" + server.Port + "/foo3").ConfigureAwait(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
server.LogEntries.Should().BeEmpty();
|
||||||
|
|
||||||
|
server.Stop();
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WireMockServer_Admin_WatchStaticMappings()
|
public void WireMockServer_Admin_WatchStaticMappings()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user