Log4Net:如何获取Logger?
我已经使用 Log4Net 几个月了,我为每个类创建一个新的 Logger 作为成员变量,如下所示:
// Member variables
private readonly ILog m_Logger = LogManager.GetLogger("MyClass");
然后我从记录的类中的每个方法调用记录器,如下所示:
// Initialize
m_Logger.Info("MyClass.MyMethod() invoked.");
...
m_Logger.Debug("MyClass.MyMethod() did something...");
...
m_Logger.Info("MyClass.MyMethod() completed.");
有什么理由不使用这种方法,还是有更好的方法来设置记录器?感谢您的帮助。
I have been using Log4Net for several months, and I create a new Logger as a member variable for each class, like this:
// Member variables
private readonly ILog m_Logger = LogManager.GetLogger("MyClass");
Then I invoke the logger from each method in the class that logs, like this:
// Initialize
m_Logger.Info("MyClass.MyMethod() invoked.");
...
m_Logger.Debug("MyClass.MyMethod() did something...");
...
m_Logger.Info("MyClass.MyMethod() completed.");
Is there any reason not to use this approach, or is there a better way to set up the logger? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的记录器可能应该是静态的,并且您可以利用其他覆盖,例如使用类型:
为了提高性能,您还应该在调用适当的日志函数之前检查您所处的日志级别。例如:
上面的例子还展示了使用反射来获取方法名称。
Your logger should probably be static, and you can take advantage of other overrides, such as using the type:
For improved performance, you should also check the log level you are in before calling the appropriate log function. For example:
The above example also shows using reflection to get the method name.