serilog-contrib / SerilogSinksInMemory

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

Output template not used #10

Closed rgwood closed 3 years ago

rgwood commented 3 years ago

Hi, thanks for writing this!

I was looking through the InMemorySinkExtensions code, and I noticed that although an output template string is defined, it's never actually used. Possible something got missed in a refactoring?

https://github.com/sandermvanvliet/SerilogSinksInMemory/blob/master/src/Serilog.Sinks.InMemory/InMemorySinkExtensions.cs#L24

sandermvanvliet commented 3 years ago

Hi @rgwood, the parameter is only there for convenience and indeed it's not used. I never intended to actually render the messages to the InMemorySink to be honest so never thought of passing it along.

If it's something you would want to be using I could have a look at passing it along.

rgwood commented 3 years ago

Gotcha. I ended up writing my own sink which does rendering.

brentarias commented 3 years ago

I'd like to see the outputTemplate be used by RenderMessage(). Can we bring back this issue/request?

sandermvanvliet commented 3 years ago

@brentarias let me investigate what is involved to get the rendering to work because as I understand it this is specific to the output sink in use (so for example the Console output sink).

brentarias commented 3 years ago

Cool! Currently my unit tests assert on the log entries, but I also have each test render all messages simply so that I can "debug peek" at them to see if the sum-total of all the log messages have the right look and format. Without the outputTemplate, I cannot tell at a glance if developers have been setting the correct log levels. Granted, I can also have these same log entries sent to a file...but there are three issues. First I am already "workflow" focused on the unit test itself. I don't want to chase a separate window to find and open the newest log file. Second, I'd prefer that my unit tests are not creating such log files. Third, I'm also interested asserting on the log levels. For example, if my test expects an exception to have occurred, I'd like to assert that one of the log entries was at "error" log level.

sandermvanvliet commented 3 years ago

If you want to verify the level of the log message you can do that using the companion Assertions package like so:

InMemorySink.Instance
   .Should()
   .HaveMessage(“my test message”)
   .Appearing().Once()
   .WithLevel(LogEventLevel.Error);

Perhaps that can already help out in your scenario.

See here for more details: https://github.com/sandermvanvliet/SerilogSinksInMemory#asserting-a-message-has-a-certain-level

brentarias commented 3 years ago

I'm more in need of this:

InMemorySink.Instance
  .Should()
  .HaveMessage()   //I don't care about the text.
  .Appearing().Once()
  .WithLevel(LogEventLevel.Error);

I don't think the library currently allows that. If the output template were present, I'd have the means to make this work.

sandermvanvliet commented 3 years ago

That’s indeed not supported through the assertion helpers however you can do this:

InMemorySink.Instance
  .LogEvents
  .Should()
  .OnlyContain(logEvent => logEvent.Level == LogEventLevel.Error);

which should give you what you need.

sandermvanvliet commented 3 years ago

@brentarias version 0.6.0 has some improvements around asserting messages that you might find interesting