Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

The venerable log4net library enables cheap instrumentation with configured logging levels, so logs are only written if the log call is on or above the active level. However, the evaluation of the log message always takes place, so there is some performance hit even if the log is not actually written. You can get over this by using delegates for the log message, which are only evaluated based on the active log level:

public static void Log(LogLevel level, Func<string> fMessage)

{

if (IsLogLevelEnabled(level))

{

LogInternal(level, fMessage.Invoke());

}

}

Making the delegate call with a lambda expression makes the code easy to read, as well as giving a performance saving:

Logger.Log(LogLevel.Debug,

() => string.Format("Time: {0}, Config setting: {1}",

DateTime.Now.TimeOfDay,

ConfigurationManager.AppSettings["configValue"]));

For simple log messages, the saving may be minimal, but if the log involves walking the current stack to retrieve parameter values, it may be worth having. The sample above writes the current time and a configuration value to the log, if set to Debug. With the log level set to Warn, the log isn't written. Executing the call 1,000,000 times at Warn level consistently takes over 3.7 seconds if the logger call is made directly, and less than 0.08 seconds if the Lambda delegate is used:

With a Warn call, the log is active and the direct and Lambda variants run 5,000 calls in 8.6 seconds, writing to a rolling log file appender:

I've added the logger and test code to the MSDN Code Gallery sample: Lambda log4net Sample, if you're interested in checking it out.

Posted on Wednesday, April 15, 2009 11:08 PM Code Snippet , Code Gallery | Back to top


Comments on this post: Making Logging Cheaper with Lambda Expressions

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Nice, however one of the advantages of log4net over some other logging libraries is the loglevel can be simply queried before writing. It's already good practice to use code like
if (log.IsWarnEnabled) log.Warn("Warning!");

An expression based version is certainly the way to go for loging interfaces that don't let you know the loglevel (e.g. Enterprise Blechs).
Left by andyclap on Apr 17, 2009 11:09 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
There's a slightly different implementation at http://www.beefycode.com/post/Extension-Methods-for-Deferred-Message-Formatting-in-Log4Net.aspx. One of the comments notes that "the method of origin for the log messages ends up being the extension method, rather than the method it is called from."

Does your approach somehow avoid this problem?
Left by jessepelton on Jan 21, 2010 6:34 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Great post with a lot of very useful resources. Thanks for sharing! . I found a lot of worthy things from this post. Thanks a lot for sharing. Keep blogging. Looking forward to reading your next post.
Left by juniper firewall management on Oct 07, 2010 9:26 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Logging is an important part of most applications, whether it's for logging performance metrics or causality data. Concise and to the point. It helped me understand it better.
Left by antiaging on Nov 22, 2010 3:13 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
This is common to make use of a either a two or a three tier architecture, with one of the layers a data access layer. This information is concise and to the point. It helped me understand it better.
Left by Refinance Mortgage on Dec 08, 2010 8:40 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Many thanks for the tutorial. It helped me a lot
Left by Pamela on Jan 26, 2011 8:39 PM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Logging is an integral part of most applications. I trust you realize that this code wasn't specific for websites, it's an issue in both website code.
Left by platinum protection on Jan 28, 2011 9:03 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Logging is an essential part of many applications. It is either for logging performance metrics or causality data. You just be to the point as it will help in understanding better.
Left by Payday loan company on May 04, 2011 1:40 PM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
This is a very informative article.I was looking for these things and here I found it.
Left by 642-642 Testking on May 20, 2011 11:59 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
It is nice to find a site about my interest. My first visit to your site is been a big help. Thank you for the efforts you been putting on making your site such an interesting and informative place to browse through. I'll be visiting your site again to gather some more valuable information. You truly did a good job.
Left by car title loan on Jun 14, 2011 9:17 PM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Good informative post. I will visit your site often to keep updated.
Left by rim financing on Jun 14, 2011 9:18 PM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Just would like to thank you for this special read. I definitely savored every little bit of it including all the comments and I have you bookmarked to check out new stuff you post.
Left by black rims on Jul 02, 2011 11:20 AM

# re: Making Logging Cheaper with Lambda Expressions
Requesting Gravatar...
Making logging cheaper with lambda expressions is good but its quite difficult.At first, I was getting an error, but then I followed your instructions exactly it worked!
Left by Fast loans on Jul 12, 2011 1:34 PM

# thanks
Requesting Gravatar...
It is a nice article.This site has lots of advantage.I found many interesting things from this site.It helps me many away. So many many thanks for sharing this information.I absolutely agree with you and thank you for pointing out several relevant and important examples. Several blog contributors have written excellently on this topic.Thanks for these sharing.
Left by healthcare it consultant on Sep 12, 2011 4:30 PM

# Online Marketing Company Singapore
Requesting Gravatar...
These tips to make the loggin cheaper is very interesting and it will trace our log time in the system. .Net have made the development very easy for the students to learn.
Left by Online Marketing Company Singapo on Dec 21, 2011 6:54 AM

# nice post
Requesting Gravatar...
I like this post it was very intresting to read
Left by Writing Blog on May 07, 2012 5:49 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net