Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6890113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:14:26+00:00 2026-05-27T06:14:26+00:00

So my problem is about logging, and how to handle the log statements which

  • 0

So my problem is about logging, and how to handle the log statements which can have influence on your code and your runtime behavior.

Log files… every program should write those for propper problem solving but how to do it right?

The most log-statements are verry expensive to get because they should provide usefull information and they are always built even the logging is disabled totaly.

The logging can be configured by xmls, inCode or in some settings etc. but this doesn’t solve the string building problem.

For example the following code is always loading a huge lazy loaded tree which would never be loaded totaly during normal execution.

The whole tree is only created for this logging statement to be displayed in the log file

(Ok, I know…, but this is just a replacement for all the complex logging methods that exists in the most programs which should never be executed during normal release execution)

public void SomeMethod(){
    logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
}

The Method someObject.GetHeavyDescriptionFromLazyTree() is always invoked even if logging is turned off, so there are some common solutions for this problem like:

public void SomeMethod(){
#if DEBUG
    logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
#endif       
}

public void SomeMethod(){
    if(logger.DoLogDebug())
        logger.Debug(someObject.GetHeavyDescriptionFromLazyTree());
}

I think it’s clear that the #if DEBUG compiler flag is no solution for productive code.
If I use the logger.DoLogDebug() there would be excessive code for checking if the logging is enabled… so this can’t be the solution either.

I thought the ConditionalAttribute could help but it’s also bound to the compiler flags and it doesn’t deactivate the call to GetHeavyDescriptionFromLazyTree

[Conditional("DEBUG")]  
public void Debug(string debugMessage) 
{ 
    Console.WriteLine(debugMessage); 
} 

So I’m looking for a logging solution which

  • is not excessive currupting my source code with debug statements
  • where the logging level is decided during runtime not at compiletime
  • where the statements are only resolved if neccessary (logLevel > definedLogLevel)

Extra loglines for the best answer 😉

Edited: I’m looking for a solution in .NET 2.0

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:14:27+00:00Added an answer on May 27, 2026 at 6:14 am

    One approach to ‘lazy logging’ is to use an expression (to create a function delegate that returns the message) rather than using direct evaluation:

    logger.Debug(() => someObject.GetHeavyDescriptionFromLazyTree());
    

    This has its own downsides (construction of the delegate), but it can be cheaper than the expensive method. Typically you would provide this as an overload and only use it in the case where you know evaluating the expression is expensive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got a strange problem about in_array recently which I cannot understand. e.g.
I have a problem about div position relative alignment. I want the second div
I have a little problem about using jQuery (I really do not know jQuery
I have hit upon this problem about whether to use bignums in my language
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I am using MFC CFile Seek function. I have a problem about Seek out
The problem is not about randomness itself (we have rand), but in cryptographically secure
Recently I've faced such problem. I'm working on the application, in which logging is
Being frustrated about fragment behavior, I started doing some testing. I have one activity
I would like to ask you a question about logging provided jars. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.