开发者

Log4Net and memory consumption

开发者 https://www.devze.com 2023-02-04 04:56 出处:网络
I see that LogNet is typically instantiated with a static variable: static logger = LogManager.GetLogger(frame.GetMethod().DeclaringType);

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 o开发者_运维百科f 300 logging static variables? Seems strange.


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.


You don't need/have to use a static variable. Typically my classes will look something like

public class Foo
{
   private ILog Logger;

   public Foo()
   {
       Logger = LogManager.GetLogger(GetType());
   }

   //rest of class here

 }

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消