Skip to content

Log messages via ILogger connected to Serilog not appearing in the test output #3089

@stigrune

Description

@stigrune

I noticed that our log messages sent through an ILogger connected to Serilog are not displayed in the Standard Output for tests. I'm trying to determine if this is a misunderstanding on my part, or maybe some kind of conflict between Serilog's console outputter and TUnit's console interceptor? While I understand this may not be a direct issue with TUnit, Im reaching out via this repository/issue to see if others have experienced similar situations and have found any working solutions.

I also noticed that the static Serilog logger configured and message logged on line L3-4 are visible in the test output, maybe something to do with log messages and test correlation?

Repository with minimal example code can be found here.

  1. The example code is created by using the TUnit ASP.NET Core Project template.
  2. Then I followed the Serilog instructions.
  3. Finally I injected the ILogger into the ping endpoints anonymous function, and added a log message.

I also tested the traditional controller based API setup with the same results.

How to trigger

In the code, you can toggle between only the default Microsoft ILogger and ILogger connected with Serilog by commenting out AddSerilog() on L13.

When running the application and calling the endpoint, both Serilog and the default ILogger logs the message as expected to the console. I can see the log message with both setups.

But when calling the same endpoint from the Test() with Serilog enabled, the message is not part of the test output.

Expected behavior: Logging to Console with Serilog should also show messages in the test standard output.

Running Test With Serilog enabled - Not as expected
Image

Running Test without Serilog - As expected
Image

Running application with Serilog enabled - As expected
Image

Running application without Serilog - As expected
Image

Side note: I suspect a custom Serilog Sink that uses the TUnit TestContext could solve this. But since Serilog is so popular and I couldnt find anyone else running into this, I figured Id ask here first. Is there maybe a more straightforward solution I'm missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions