serilog-contrib / SerilogSinksInMemory

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

Setting the output message formatter #30

Closed SoggyBottomBoy closed 5 months ago

SoggyBottomBoy commented 5 months ago

Am I right in thinking the InMemorySink does not provide an option to set the outputFormatter? Because of the async nature of the InMemorySink.Instance I have had to register an instance of a singleton in my DI container. I want to include the Exception message in the RenderMessage but can't figure out how to do this.

This is the code I have

InMemorySink logSink = new();
services.AddSingleton(logSink);

_ = services.AddLogging(builder =>
{
    var logger = new LoggerConfiguration()
        .MinimumLevel.Information()
        .WriteTo.Sink(logSink, Serilog.Events.LogEventLevel.Information)
        .CreateLogger();

    builder.AddSerilog(logger);
});

A ConsoleSink for example takes a formatter as part of its constructor, however the same does not existing for the InMemorySink.

Is there any way to set the message formatter. It also appears that the extension method does nothing with the formatter? https://github.com/serilog-contrib/SerilogSinksInMemory/blob/master/src/Serilog.Sinks.InMemory/InMemorySinkExtensions.cs

sungam3r commented 5 months ago

I think this is by design since memory sink just buffers log events. There is nothing to format because there is no output message.

sandermvanvliet commented 5 months ago

That’s correct.

The sink isn’t meant to verify log message formatting, it’s meant to verify that a message is logged in a certain way. For example that the message template is correct and that the right properties are set on it.

SoggyBottomBoy commented 5 months ago

Ok but the RenderMessage therefore can not be configured, and the extension method includes this option but in the code is simply not used which is misleading.