serilog-contrib / SerilogSinksInMemory

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

What is the best way to check for properties in multiple messages? #17

Closed xavierjohn closed 3 years ago

xavierjohn commented 3 years ago

What is the best way to check for properties in multiple messages?

I would like to write something similar to

InMemorySink.Instance.Should()
            .HaveMessage("HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms")
            .WithLevel(LogEventLevel.Information)
            .Appearing().Times(2)
            .WithProperty("CorrelationId").BeEquivalentTo(new string[] { "CorrelationId1", "CorrelationId2"});  // Something like this.
xavierjohn commented 3 years ago

My current solution is

        InMemorySink.Instance.Should()
            .HaveMessage("HTTP {RequestMethod} {RequestPath} responded {StatusCode} in {Elapsed:0.0000} ms")
            .WithLevel(LogEventLevel.Information)
            .Appearing().Times(2)
            .Subject
            .Select(r => r.Properties["CorrelationId"].ToString().Trim('"'))
            .Should()
            .BeEquivalentTo(new string[] { "CorrelationId1", "CorrelationId2" });
sandermvanvliet commented 3 years ago

@xavierjohn thanks for the suggestion!

I've introduced the following in #18:

_logger.Information("Message with {Property}", "val1");
_logger.Information("Message with {Property}", "val2");

InMemorySink.Instance
                .Should()
                .HaveMessage("Message with {Property}")
                .Appearing().Times(2)
                .WithProperty("Property")
                .WithValues("val1", "val2");

This will be available with version 0.7.0 of the assertions package which should be on NuGet shortly.