The point is: you shouldn't attempt to format the string until you're sure that it will be necessary. So this should be deferred to the point where loggers and filters agree that there's something to be written.
There was a page somewhere on the log4net site which explained it in more detail.
Jarek
----- Original Message ----- From: "Hart, Leo" <[EMAIL PROTECTED]>
To: "Log4NET User" <[email protected]>
Sent: Friday, March 18, 2005 4:39 PM
Subject: RE: Log4Net only for the committed techies
Why not? It makes it so much fun to read! ;)
-----Original Message----- From: Ron Grabowski [mailto:[EMAIL PROTECTED] Sent: Friday, March 18, 2005 10:37 AM To: Log4NET User Subject: RE: Log4Net only for the committed techies
Please don't write code like that :)
--- "Hart, Leo" <[EMAIL PROTECTED]> wrote:For the garbage problem, can't you just use a StringBuilder there to avoid that? For instance:
Log.Debug(new StringBuilder("My name is[").append(name).append("].");
-----Original Message----- From: Jaroslaw Kowalski [mailto:[EMAIL PROTECTED] Sent: Friday, March 18, 2005 10:12 AM To: Log4NET User Cc: [EMAIL PROTECTED] Subject: Re: Log4Net only for the committed techies
Hi Ron,
Some answers to your questions inside:
> Which leads me to believe that people often use less than 3 > placeholders. Do people really use 10 or 15 placeholders at a time?
> Yuck. I don't think there's anything wrong with this: > > log.Debug("My name is [" + name + "].");
The problem is: garbage. When you write the code above you'll have at least one (perhaps two) memory allocations. This code:
log.Debug("My name is [{0}].", name)
has zero memory allocation for non-logging case. This can be very useful in tight loops. With log4net you have to comment out your logging code. With NLog there's no need to do that - you just disable the Debug level for particular logger and you're done.
NLog is optimized for one placeholder (zero-garbage) but in the future this may be changed to support more than one parameter.
To do the same with log4net efficiently you'd have to:
if (log.IsDebugAllowed) { log.Debug("My name is [" + name + "]."); }
>> LogLevel level = LogLevel.Debug; >> >> logger.Log(level, "aaaa"); > > Is this high level programmer? I'd be curious where this may be > useful. I see it as another local variable that I need to keep track > of.
It may be useful when you're wrapping NLog with some other logging layer, which is not so uncommon. Otherwise you'd have to use a switch:
switch (...) { case Debug: log.Debug(...); break; case Error: log.Error(...); break; case Fatal: log.Fatal(...); break; case Info: log.Info(...); break; ... }
Jarek
