Friday, May 18, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Re: Checking IsDebugEnabled before calling Debug()?
Prev Next
You are not authorized to post a reply.

Author Messages
Tom Nichols

11/18/2005 3:49 PM  

Hi,

I was going through the examples and saw this:
       // Log a debug message. Test if debug is enabled before
       // attempting to log the message. This is not required but
       // can make running without logging faster.
       if (log.IsDebugEnabled) log.Debug("This is a debug message");

My question is, why not just check this in LogImpl.Debug() , i.e.
       virtual public void Debug(object message)
       {
               if( this.IsDebugEnabled ) Logger.Log(ThisDeclaringType,
m_levelDebug, message, null);
}

And then of course do the same for DebugFormat, Info, Warn, etc. etc.
I haven't dug much into the code but I'm interested in using the
framework.  It just seems to me that if you really get a performance
enhancement from testing if IsDebugEnabled before calling it, it would
make more sense to save that step everywhere you make that Debug()
call in paractice.

I'm guessing there's a good reason why, but could someone tell me?

Thanks.
-Tom

Göran Roseen

11/18/2005 4:06 PM  

Yes, the reason is that sometimes it takes a while to generate the argument
to log.Debug(). If your code looks something like:

    log.Debug(CallReallySlowComponentAndGenerateAMessage());

you would of course benefit from using

    if (log.IsDebugEnabled)
        log.Debug(CallReallySlowComponentAndGenerateAMessage());

instead, since the time-consuming generation of the message will not occur.
If you only log string literals, I suppose there is no point in using this
construct.

/Göran

Castrianni, Chris {PBSG}

11/18/2005 4:07 PM  

Good morning Tom,

My understanding is that you should use IsXXXEnabled for situations
where "message" is computationally expensive to create.

--Chris

Dru Sellers

11/18/2005 4:20 PM  

I use the boolean check do other things as well.

if(this.IsDebugEnabled){
        string message = ExpensiveMessaegCreation();
        Logger.Log(ThisDeclaringType, m_levelDebug, message, null);
}

dru

Matthew Brown

11/18/2005 5:19 PM  
"computationally expensive", i.e., concatenating strings

Tom Nichols

11/18/2005 5:23 PM  

Okay, thanks for the clarification everyone.
How computationally expensive is something simple like:

Log.Debug( "Couldn't connect to: " + connectionString );

That's the majority of what my messages would look like.

Thanks!
-Tom

Castrianni, Chris {PBSG}

11/18/2005 5:57 PM  
> "computationally expensive", i.e., concatenating strings
 
Actually, no.  I would not consider concatenating strings to be computationally expensive (unless we're talking a plethora of strings).  A better example might be invoking a web service to retrieve a particular value that you wish to log.  If the level at which the message will be logged isn't even enabled, then there is no need to invoke the web service to retrieve the value.
You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Re: Checking IsDebugEnabled before calling Debug()?



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement