Fix unsubscribe from LogEntriesChanged event handler (#872)

* Fix unsubscribe from LogEntriesChanged event handler

* .

* f
This commit is contained in:
Stef Heyenrath
2023-01-19 14:23:38 +01:00
committed by GitHub
parent 20eb37b0c8
commit b2a8178161
4 changed files with 172 additions and 107 deletions

View File

@@ -14,26 +14,12 @@ namespace WireMock.Server;
public partial class WireMockServer
{
/// <inheritdoc cref="IWireMockServer.LogEntriesChanged" />
/// <inheritdoc />
[PublicAPI]
public event NotifyCollectionChangedEventHandler LogEntriesChanged
{
add
{
_options.LogEntries.CollectionChanged += (sender, eventRecordArgs) =>
{
try
{
value(sender, eventRecordArgs);
}
catch (Exception exception)
{
_options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message);
}
};
}
remove => _options.LogEntries.CollectionChanged -= value;
add => _logEntriesChanged += value;
remove => _logEntriesChanged -= value;
}
/// <inheritdoc cref="IWireMockServer.LogEntries" />
@@ -90,4 +76,24 @@ public partial class WireMockServer
return false;
}
private NotifyCollectionChangedEventHandler? _logEntriesChanged;
private void LogEntries_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (_logEntriesChanged is { })
{
foreach (var handler in _logEntriesChanged.GetInvocationList())
{
try
{
handler.DynamicInvoke(this, e);
}
catch (Exception exception)
{
_options.Logger.Error("Error calling the LogEntriesChanged event handler: {0}", exception.Message);
}
}
}
}
}