Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add WithLogger(ILogger) builder API #1100

Merged
merged 17 commits into from
Mar 11, 2024

Conversation

0xced
Copy link
Contributor

@0xced 0xced commented Jan 28, 2024

What does this PR do?

This pull request moves the logger configuration from the static TestcontainersSettings class to the AbstractBuilder class.

Before:

TestcontainersSettings.Logger = logger;

After:

new TBuilderEntity().WithLogger(logger).Build()

Why is it important?

This will enable a seamless integration with xUnit.net (both with ITestOutputHelper and with IMessageSink).

Related issues

Fixes #996

Copy link

netlify bot commented Jan 28, 2024

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit 66ec29f
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-dotnet/deploys/65ef31dc4fd9ec0008539e1f
😎 Deploy Preview https://deploy-preview-1100--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@0xced
Copy link
Contributor Author

0xced commented Feb 10, 2024

Continuing discussion from #1093 (comment) to here (where it belongs).

The PR does not contain the Xunit project, right? I think it is a good idea!

Indeed this pull request does include the new Xunit project. I will submit another pull request dedicated to the new Xunit integration.

A lot of developers will benefit from it. I have not taken a close look into the PR that involves the changes to the logging implementation yet. I took a quick look, and it is quite big.

It's big because it removes the ILogger parameter from all containers in all modules but the actual change in AbstractBuilder`4.cs is suprisingly small.

IIRC, I proposed to split it into two steps: one that provides an internal WithLogger(ILogger) builder method, and another that makes the necessary changes regarding logging the container runtime information and making the method public. After that, we can add the dedicated Xunit, package. WDYT? Splitting it makes reviewing much easier.

I tried but I don't see how it's possible to split in two steps. Again, if you ignore the required changes in the modules the core feature diff is not that big.

@0xced 0xced force-pushed the ILoggerBuilder branch 3 times, most recently from 952ffe5 to ccaa317 Compare February 12, 2024 20:27
@HofmeisterAn
Copy link
Collaborator

I haven't had the time yet, but I will review it in the next few days.

@0xced
Copy link
Contributor Author

0xced commented Feb 22, 2024

Great, I'll refrain from submitting new pull requests in the meantime. 😉

…ders

This will enable a seamless integration with xUnit.net (both ITestOutputHelper and IMessageSink)

Fixes testcontainers#996
Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me some time to review everything, and as I got closer to completion, I identified and refactored a couple of crucial parts. As mentioned in a previous comment, I would like to avoid logging the runtime information either too frequently (as is the case now) or just once in cases where developers cannot easily find it. While it still requires some polishing, I believe we are very close now. I have included tests to ensure that the runtime information is only logged once per logger instance (LoggerTest). This guarantees the presence of the runtime information in cases where developers use the ITestOutputHelper, which, IMO, looks quite good. I can provide more details and necessary follow-ups tomorrow, but for today, I am finished and tired. If you have some time to review the changes as well, that would be awesome and much appreciated. I am certain that we can make further improvements.

@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Mar 2, 2024
@HofmeisterAn HofmeisterAn changed the title Make the logger configurable with a new WithLogger method on the builders feat: Add WithLogger(ILogger) builder API Mar 2, 2024
@HofmeisterAn
Copy link
Collaborator

HofmeisterAn commented Mar 2, 2024

The PR is ready for a final review. @0xced your feedback is much appreciated (no hurry). I am happy to merge the PR afterward.

@0xced
Copy link
Contributor Author

0xced commented Mar 5, 2024

Awesome, I'll try to have a look this week.

@0xced
Copy link
Contributor Author

0xced commented Mar 10, 2024

I just pushed 1ee0e3e which uses the configured ILogger for the resource reaper too.

I think the configured logger should also be passed to the PortForwardingContainer, I'm looking into it…

@HofmeisterAn
Copy link
Collaborator

I think the configured logger should also be passed to the PortForwardingContainer, I'm looking into it…

We can also change this with a follow-up PR. Probably, we simply need to replace the singleton property with a method that provides an ILogger overload.

@0xced
Copy link
Contributor Author

0xced commented Mar 11, 2024

OK, let's take care of PortForwardingContainer in a follow up PR.

I see that you removed the structured logging of runtime information that I had introduced. I re-introduced it in a slightly improved way in fef7faa. I think it makes the LogContainerRuntimeInfoAsync implementation much easier to read and also it's the right way to do structured logging. But if you don't like it, just drop the commit.

One last question: why did you introduce an empty TestcontainersSettings static constructor in e535cbe?

@HofmeisterAn
Copy link
Collaborator

I think it makes the LogContainerRuntimeInfoAsync implementation much easier to read and also it's the right way to do structured logging. But if you don't like it, just drop the commit.

Can we please revert it? I really do not like it, and structured logging does not have a lot of benefits here. I would like to remove Logging anyway in the future. Logging the message immediately is much easier. Happy to merge the pull request afterward and publish a beta version.

@0xced
Copy link
Contributor Author

0xced commented Mar 11, 2024

OK fine, I’ll revert.

What about the empty TestcontainersSettings static constructor?

Happy to merge the pull request afterward and publish a beta version.

Could you please have a look at #1139 before publishing? I promise it’s much simpler than this one. 😉

@HofmeisterAn
Copy link
Collaborator

What about the empty TestcontainersSettings static constructor?

This guarantees lazy initialization (probably not really important here), see beforefieldinit.

Could you please have a look at #1139 before publishing? I promise it’s much simpler than this one. 😉

Yes, we can merge this before publishing a beta version too.

@0xced
Copy link
Contributor Author

0xced commented Mar 11, 2024

I just pushed 1bf2f96 which removes the structure logging and I extracted the logging code into a new method for improved readability, and also catching only the exceptions when running GetSystemInfoAsync and GetVersionAsync.

This guarantees lazy initialization (probably not really important here), see beforefieldinit.

Wow, I had no idea, gotta agree with Mike Marynowski!

[...] controlling the timing of when you want a class to run its static initialization shouldn't be controlled entirely by the presence or absence of a static constructor, that's just so odd...

I think it's now ready to be merged. 😀

@HofmeisterAn HofmeisterAn merged commit 1cfc850 into testcontainers:develop Mar 11, 2024
11 checks passed
@0xced 0xced deleted the ILoggerBuilder branch March 11, 2024 17:59
@HofmeisterAn HofmeisterAn added the breaking change Causing compatibility issues label Mar 11, 2024
StefH pushed a commit to WireMock-Net/WireMock.Net that referenced this pull request Jun 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Causing compatibility issues enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Enhancement]: ILogger should be injected into all AbstractBuilder subclasses
2 participants