Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refactor log4net statements in c#?
    primarykey
    data
    text
    <p>Ok so after reading danben's answer on this <a href="https://stackoverflow.com/questions/2230180/whats-log4j-actually-doing-when-we-turn-on-or-off-some-log-places">post</a>, I guess I'm convinced of the need for writting this kind of code, atleast in a lot of cases. My managers seem to be agreeing too.</p> <pre><code>if (log.IsDebugEnabled) log.Debug("ZDRCreatorConfig("+rootelem.ToString()+")"); if (log.IsInfoEnabled) log.Info("Reading Configuration . . ."); </code></pre> <p>The problem with it is it bugs the heck out of me seeing all these if statements placed everywhere just to do a simple log statement. </p> <p>My question is, how might we refactor this into a class without reproduceing the performance problem of having to evaluate the arguments to the log method?</p> <p>Simply putting it in a class as a static method doesn't help, because when you pass the Object message it still has to evaluate the argument:</p> <pre><code>public class LogHelper { public static Info(ILog log, Object message) { if(log.IsInfoEnabled) { log.Info(message); } } } </code></pre> <p>C# apparently doesn't support forcing a method to be inline, so that solution isn't available. MACROs are not supported in C#. What can we do?!?!</p> <p>UPDATE: Thanks for the replies, I have not forgot about this one; it's just low priorty on my list right now. I will get to it and award the answer once I get caught up a bit. Thanks.</p> <p>Another UPDATE:<br> well ... I still haven't look at this closly yet, and both of you deserve the correct answer; but I awarded Tanzelax the answer because I agree, I think they will be automatically inlined. The link he posted does good job of convincing me I shouldn't worry too much about this right now, which is also good lol. I'll still look at those lambda things later. Thanks for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload