serilog-contrib / SerilogSinksInMemory

In-memory sink for Serilog to use for testing
MIT License
53 stars 7 forks source link

HaveMessage throws `System.InvalidOperationException : Collection was modified; enumeration operation may not execute.` exception #16

Closed Yaroshvitaliy closed 3 years ago

Yaroshvitaliy commented 3 years ago

The following line in unit test

InMemorySink.Instance.Should().HaveMessage(message);

sometimes throws an exception as follows:

System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
  Stack Trace:
     at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Serilog.Sinks.InMemory.Assertions.InMemorySinkAssertions.HaveMessage(String messageTemplate, String because, Object[] becauseArgs)

I guess it will be fixed if .ToList is followed by .Where: https://github.com/sandermvanvliet/SerilogSinksInMemory/blob/master/src/Serilog.Sinks.InMemory.Assertions/InMemorySinkAssertions.cs#L23

sandermvanvliet commented 3 years ago

@Yaroshvitaliy thanks for reporting this issue!

I've addressed this in #18 and the fix should be available as version 0.7.0 of the assertions package on NuGet shortly.