Observable logs (#51)

* observable log entries

* event test
This commit is contained in:
deeptowncitizen
2017-10-07 09:05:02 -04:00
committed by Stef Heyenrath
parent 9c55ff5ea6
commit 2d39a18b70
4 changed files with 87 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
@@ -161,13 +162,20 @@ namespace WireMock.Owin
if (_options.MaxRequestLogCount != null)
{
_options.LogEntries = _options.LogEntries.Skip(_options.LogEntries.Count - _options.MaxRequestLogCount.Value).ToList();
var amount = _options.LogEntries.Count - _options.MaxRequestLogCount.Value;
for (var i = 0; i < amount; i++, _options.LogEntries.RemoveAt(0)) ;
}
if (_options.RequestLogExpirationDuration != null)
{
var checkTime = DateTime.Now.AddHours(-_options.RequestLogExpirationDuration.Value);
_options.LogEntries = _options.LogEntries.Where(le => le.RequestMessage.DateTime > checkTime).ToList();
for (var i = _options.LogEntries.Count - 1; i >= 0; i--)
{
var le = _options.LogEntries[i];
if (le.RequestMessage.DateTime <= checkTime)
_options.LogEntries.RemoveAt(i);
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using WireMock.Logging;
using WireMock.Matchers;
@@ -15,7 +16,7 @@ namespace WireMock.Owin
public IList<Mapping> Mappings { get; set; }
public IList<LogEntry> LogEntries { get; set; }
public ObservableCollection<LogEntry> LogEntries { get; set; }
public int? RequestLogExpirationDuration { get; set; }
@@ -24,7 +25,7 @@ namespace WireMock.Owin
public WireMockMiddlewareOptions()
{
Mappings = new List<Mapping>();
LogEntries = new List<LogEntry>();
LogEntries = new ObservableCollection<LogEntry>();
}
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using JetBrains.Annotations;
using WireMock.Logging;
using WireMock.Matchers.Request;
@@ -11,6 +12,28 @@ namespace WireMock.Server
{
public partial class FluentMockServer
{
/// <summary>
/// Log entries notification handler
/// </summary>
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
lock (((ICollection) _options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged += value;
}
}
remove
{
lock (((ICollection)_options.LogEntries).SyncRoot)
{
_options.LogEntries.CollectionChanged -= value;
}
}
}
/// <summary>
/// Gets the request logs.
/// </summary>