Log4Net 和内存消耗
我看到 LogNet 通常是用静态变量实例化的:
static logger = LogManager.GetLogger(frame.GetMethod().DeclaringType);
愚蠢的问题,但是:
1)这不是有点烦人吗——即。我的代码的每个日志记录片段中都必须有这种混乱的调用。
2)这不会产生大量的内存开销---即。我的每一个类都将与一个单独的日志记录类相关联。使用日志记录的 300 个类将强制使用 300 个日志记录静态变量?看起来很奇怪。
I see that LogNet is typically instantiated with a static variable:
static logger = LogManager.GetLogger(frame.GetMethod().DeclaringType);
Stupid question, but:
1) Is this not a bit annoying --- ie. I must have this mess of a call in each logging piece of my code.
2) Does this not generate a lot of memory overhead --- ie. Every single one of my classes will be associated with a seperate logging class. 300 classes that use logging will force the use of 300 logging static variables? Seems strange.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
log4net 维护一个记录器存储库,您无需将它们设为静态。推荐的方法是为每个类拥有一个命名记录器,您无需担心内存中拥有数百个记录器的“开销”。如果这是一个问题,您可能需要完全删除日志记录。
日志记录很混乱,即它会用不属于业务的内容使您的代码变得混乱。面向方面的编程可能会帮助您那里。
log4net maintains a repository of loggers, there is no need for you to make them static. Having a named logger per class is the recommended way, you do not need to worry about the "overhead" of having a few hundred loggers in memory. If that were a problem, you probably would need to remove logging altogether.
Logging is messy i.e. it clutters your code with stuff that is not really part of the business. Aspect orient programming might help you there.
您不需要/必须使用静态变量。通常,我的类看起来像这样
这是相当干净的,除非我遇到内存问题,否则早期优化应用程序的内存使用是没有意义的。然而,并非我的所有类都执行日志记录,因此只有那些执行/需要的类才会在其中包含这段代码。如果您的每个类都在记录日志,那么听起来您可能记录过多,或者您正在处理一个相当大的应用程序(基于您有 300 个类写入日志的事实)。如果是后者,那么 300 个日志记录类实例可能并不是内存使用占用的重要部分。
您可以选择将记录器设置为命名实例
ILog Logger = LogManager.GetLogger("MyNamedLogger");
,并在多个类中使用此“命名”ILog 实例。请注意,由于日志消息通常的写入方式,您将失去所记录消息的一些用处。您当然可以通过几种不同的方式来弥补这一点。You don't need/have to use a static variable. Typically my classes will look something like
This is fairly clean and until I have a memory problem, there's no point in early optimizing memory usage of the application. However not all of my classes perform logging, so only those that do/need to will have this bit of code in them. If everyone of your classes is logging then it sounds like you may be over-logging, either that or you're dealing with a rather large application (based upon the fact you've got 300 classes writing out to the log). If the later is the case then 300 instances of a logging class probably isn't a significant portion of your memory usage footprint.
You do have the option of setting up your logger as a named instance
ILog Logger = LogManager.GetLogger("MyNamedLogger");
instead and using this "named" ILog instance across multiple classes. Note that you will lose some of the usefulness of the logged messages due to the way the log messages are generally written out. You could certainly compensate for that in several different ways.