Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3601 Comments | 0 Trackbacks

News

Archives

Post Categories

[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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Wednesday, April 15, 2009 11:08 PM

Feedback

# re: Making Logging Cheaper with Lambda Expressions 4/17/2009 11:09 AM andyclap
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).


# re: Making Logging Cheaper with Lambda Expressions 1/21/2010 6:34 AM jessepelton
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?

# re: Making Logging Cheaper with Lambda Expressions 10/7/2010 9:26 AM juniper firewall management
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.


# re: Making Logging Cheaper with Lambda Expressions 11/22/2010 3:13 AM antiaging
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.


# re: Making Logging Cheaper with Lambda Expressions 12/8/2010 8:40 AM Refinance Mortgage
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.

# re: Making Logging Cheaper with Lambda Expressions 1/26/2011 8:39 PM Pamela
Many thanks for the tutorial. It helped me a lot

# re: Making Logging Cheaper with Lambda Expressions 1/28/2011 9:03 AM platinum protection
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.

# re: Making Logging Cheaper with Lambda Expressions 5/4/2011 1:40 PM Payday loan company
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.


# re: Making Logging Cheaper with Lambda Expressions 5/20/2011 11:59 AM 642-642 Testking
This is a very informative article.I was looking for these things and here I found it.


# re: Making Logging Cheaper with Lambda Expressions 6/14/2011 9:17 PM car title loan
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.

# re: Making Logging Cheaper with Lambda Expressions 6/14/2011 9:18 PM rim financing
Good informative post. I will visit your site often to keep updated.

# re: Making Logging Cheaper with Lambda Expressions 7/2/2011 11:20 AM black rims
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.

# re: Making Logging Cheaper with Lambda Expressions 7/12/2011 1:34 PM Fast loans
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!

# thanks 9/12/2011 4:30 PM healthcare it consultant
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.

# Online Marketing Company Singapore 12/21/2011 6:54 AM Online Marketing Company Singapo
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.


Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: