How to reorganize log4net statements in C #?

Well, therefore, after reading danben's answer to this post , I think I am convinced of the need to write such code, in many cases. My managers agree too.

if (log.IsDebugEnabled) log.Debug("ZDRCreatorConfig("+rootelem.ToString()+")"); if (log.IsInfoEnabled) log.Info("Reading Configuration . . ."); 

The problem with this is the bug due to which I see all these if statements posted everywhere, just to make a simple log statement.

My question is, how can we reorganize this into a class without repeating the performance problem when evaluating the arguments to the log method?

Just putting it in a class, as the static method does not help, because when you pass an Object message, it still needs to evaluate the argument:

 public class LogHelper { public static Info(ILog log, Object message) { if(log.IsInfoEnabled) { log.Info(message); } } } 

C # does not seem to support forcing the method to be inline, so a solution is not available. MACRO is not supported in C #. What we can do?!?!

UPDATE: Thanks for the answers, I have not forgotten about this; it's just the lowest priorty on my list right now. I will get to him and give the answer as soon as I catch a little. Thanks.

Other UPDATE:
okay ... I still don't look at this very close, and you both deserve the right answer; but I awarded Tanzelax for the answer, because I agree, I think they will be automatically included. The link that he posted really helps to convince me that I should not worry too much about it right now, which is also good. I will continue to look at these lambda things later. Thanks for the help!

+4
source share
3 answers

If the static helper method is so simple, it should be built in automatically and will be consistent with performance.

At what level does the C # compiler or JIT optimize application code?

+3
source

One simple solution is to use lambda expressions to effectively delay the generation of messages until you need it:

 public static class LogHelper { public static void Info(this ILog log, Func<Object> messageProvider) { if(log.IsInfoEnabled) { log.Info(messageProvider()); } } } 

Call the following address:

 log.Info(() => "This is expensive: " + CalculateExpensiveValue()); 
+15
source

Just a comment about the static function of the log helper ...

If you use the LogHelper function, as you suggest, you will lose the ability to register information about the call site.

So, if you have a static helper class (without leaving aside the evaluation of the message parameters):

 public class LogHelper { public static Info(ILog log, Object message) { if(log.IsInfoEnabled) { log.Info(message); } } } 

And you use it as follows:

 public class MyClass { ILog logger = LogManager.GetLogger(<blah blah>); public void MyFunc() { logger.Info("Hello!"); } } 

If you enable “call site” logging, the site’s site information will come from your helper class: LogHelper.Info , not your real class MyClass.MyFunc .

This may not make much difference if you do not rely on registering site site information.

+1
source

Source: https://habr.com/ru/post/1301047/


All Articles