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

Substiture for ILoggerFactory.CreateLogger<T> #728

Closed
alvipeo opened this issue Aug 25, 2023 · 5 comments
Closed

Substiture for ILoggerFactory.CreateLogger<T> #728

alvipeo opened this issue Aug 25, 2023 · 5 comments
Labels
question Question on NSubstitute behaviour

Comments

@alvipeo
Copy link

alvipeo commented Aug 25, 2023

In almost any .NET project I need to have a substitute for ILoggerFactory and its probably most used method CreateLogger<T>(). It returns ILogger<T>, which is in turn:

public interface ILogger<out TCategoryName> : Microsoft.Extensions.Logging.ILogger

Any idea how could this be substituted?

@olegd-superoffice
Copy link

@alvipeo CreateLogger<T>() is an extension method so you need to substitute CreateLogger(string) (which is called from extension) instead:

var loggerFactory = Substitute.For<ILoggerFactory>();
loggerFactory.CreateLogger(null).ReturnsForAnyArgs(NullLogger<MyType>.Instance);

Or maybe just use NullLoggerFactory instead of mocking.

@dtchepak
Copy link
Member

dtchepak commented Sep 3, 2023

In addition to @olegd-superoffice's suggestion on returning a substitute/NullLogger, there are some changes in #732 that may help with checking ILogger calls.

@304NotModified
Copy link
Contributor

related: #597

@olegd-superoffice
Copy link

@alvipeo Could you please close this issue if your question was answered?

@304NotModified 304NotModified added the question Question on NSubstitute behaviour label Apr 29, 2024
@304NotModified
Copy link
Contributor

I think the question has been answered and therefore I will close this one.

Please let us know if you need further information or would like us to take another look at this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on NSubstitute behaviour
Projects
None yet
Development

No branches or pull requests

4 participants