如何重构在C#中log4net的声明? [英] How to refactor log4net statements in c#?

查看:196
本文介绍了如何重构在C#中log4net的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以读danben的回答对这个的帖子,我想我相信需要书面方式对这种代码,在ATLEAST很多情况下。我的经理人似乎同意了。

Ok so after reading danben's answer on this post, 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.

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



它的问题是错误的挫折感了我看到所有这些if语句放在任何地方正好做一个简单的日志声明。

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.

我的问题是,我们如何重构为一个类此无reproduceing不必参数评估到日志方法的性能问题?

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?

只要把它在一个类的静态方法没有帮助,因为当你传递对象的消息仍必须评估参数:

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:

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



C#显然不支持强制法内联,这样的解决方案是不可用。宏不是在C#的支持。 ?!?!我们可以做什么

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?!?!

更新:
感谢您的答复,我没有忘记这一个;这是我的名单上只是低先决现在。我会得到它,并颁发了答案,一旦我被抓了一下。谢谢

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.

另一个更新:结果
嗯...我还没有在这看closly着呢,你们俩应该得到正确的答案;但我授予Tanzelax的答案,因为我同意,我认为他们将被自动内联。他发布的链接确实说服了我,我不应该太担心,现在这种权利,这也很好笑的好工作。我还是会看那些东西拉姆达以后。感谢您的帮助!

Another UPDATE:
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!

推荐答案

如果静态辅助方法,就是这么简单,它应该自动内联,将有表现相匹配。

If the static helper method is that simple, it should be inlined automatically, and will have the performance to match.

http://stackoverflow.com/questions/650652/at-what-level-c-compiler-or-jit-optimize-the-application-code

这篇关于如何重构在C#中log4net的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆