Home > Software engineering >  How to correctly call the .NET logger.LogError method with 2x params?
How to correctly call the .NET logger.LogError method with 2x params?

Time:12-21

I'm trying to do a call to this logger.LogError method but the wrong extension method keeps getting called.

I have:

_logger.LogError("This is an error {someBool} and {someString}", someBool, someString);

and it's calling this extension method:

enter image description here

but I'm wanting to call this extension method instead:

enter image description here

How do I end up calling -that- specific extension method?

UPDATE:

As suggested below, hit F12 to goto the code ... and it is this (keep reading afterwards .. there's a catch!)

enter image description here

So at first that namespace looks like it's official ..

until I noticed the ASSEMBLY it's part of and stuff .. and it's all a custom class in a private nuget package (for this company) from another dev. CHEEKY!!!!!

So yeah - it's all a custom extension method.

I thought it was part of the official MS code. I appologize.

CodePudding user response:

You can always force the compiler by using the exact types required for one of the functions, if you don't like the automatic resolution:

_logger.LogError("This is an error {someBool} and {someString}", 
                 new object[] { someBool, someString });

That said, I never had this problem, check where this method comes from and why it gets picked over the one you prefer.

Since the method you want comes from a class the other method is not defined in, you could also call it directly, without the extension method syntax:

LoggerExtensions.LogError(
       _logger,
       "This is an error {someBool} and {someString}", 
       someBool, 
       someString);

This should rule out those other method that conflicts with what you want.

CodePudding user response:

To use the extension method you need to pass an object array.

Try to do something like:

object[] params = {var1, var2, var3};
  • Related