3

I created log method that accept string. When I want to use it I write something like:

Log(string.Format("Message {0}", AdditionalInfo));

How should I implement Log method in order to be able to use string Format but do not have to write it explicitly in method arguments:

Log("Message {0}", AdditionalInfo);

I use .net 2.0

4
  • 1
    Thank you for fast answers :) Commented Jun 10, 2011 at 9:17
  • 1
    now feel free to watch the fight :) Commented Jun 10, 2011 at 9:24
  • @Zruty: LOL, guess we can argue the whole day about this... Commented Jun 10, 2011 at 9:26
  • I hope someone who has the privileges clears this question after we're done having fun Commented Jun 10, 2011 at 9:27

5 Answers 5

8
public void Log(string formatString, params object[] parameters)
{
    Log(String.Format(formatString, parameters));
}
Sign up to request clarification or add additional context in comments.

8 Comments

This site is named for a reason :) Anyway, you are wrong. At least C# 4.0 distinguishes these overloads of Log() and do not cause infinite recursion
@Darin Not really, the functions have different parametres.
@Zruty, @Nubsis, are you sure? Did you test the code? Because I did. And I did in using C# 4.0.
I verified it before posting my answer, and it worked OK. What's the code you were testing on?
@Darin Dimitrov: Yes, you probably didn't have the Log(string) overload, which the original poster seemed to have.
|
4
public void Log(string format, params object[] args)
{
    DoTheLog(string.Format(format, args));
}

1 Comment

Ouch, failing to overload the Log method.
2
void Log(string format, params object[] args)
{
  Log(string.Format(format, args));
}

9 Comments

@Darin Dimitrov: Huh? Please explain why YOU think recursion (which is not even the case here) is bad.
This is an endless recursion. That's the problem! At least if there is no method Log(string message)
@Daniel Hilgarth: That is assuming Log(string) is not defined, but clearly the OP has the method defined...
@leppie: IMHO it should be told that this causes an endless recursion if the overload is missing, because it is not obvious to many.
@Daniel Hilgarth: It is not my problem that people cant read the question correctly, sorry.
|
1

Try something like:

public void Log(string format, params object[] arguments)
{
    string message = string.Format(format, arguments);
    // Do something with message.
}

Comments

-3

You could perhaps write a wrapper for your Log method. I hope something like this works.

public void Log(string format, object args)
{
    Do_Log(string.Format(format, args))
}

Now you can have Do_Log() as private and not expose it to consumers of your class.

1 Comment

-1: I think the question has bee answered enough, no need to just 'copy' the other responses...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.