Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ability to programmatically remove a Logger

Tags:

log4net

Is there a way to remove a Logger once it has been added? Say via:

LogManager.GetLogger("loggerName")

In my research, it would appear it is not possible. The closest I have found is the ability to call the Clear hierarchy method. This will indeed clear out the existing loggers but I would like to selectively remove them, not to mention this likely isn't the safest thing to be doing.

For some background, I am logging one file per task where there could be potentially thousands of concurrent tasks and hundreds of thousands of tasks per application lifetime. One approach is to create a Logger for each task and then remove it once the task has completed. Without the ability to selectively remove a Logger though, memory will get chewed up by the retired instances.

Of course, there are alternative designs that would work. The problem could be addressed by adding/removing Appenders and Filters as necessary to a given Logger. Also, a pool of Loggers and Appenders could be created and then configured per task.

It obviously isn't a show stopper if there is no way to remove a specific logger once it is added. I'm just curious if there is a way to delete a Logger that I may have missed?

like image 828
Adam S Avatar asked Sep 13 '25 05:09

Adam S


2 Answers

Wrapping this up since it has been open a long time. I haven't found a way to remove a logger by itself and nothing I've seen in the documentation suggests it is possible either. The solution that worked for me to share a pool of loggers that would only grow to the max amount of concurrent tasks. Each task could take a logger from the pool with an appender added specific to that task. On task completion, the appender is removed and the logger returned to the pool.

like image 106
Adam S Avatar answered Sep 15 '25 17:09

Adam S


No, it isn't possible. This is specifically mentioned in their FAQ these days:

Logger instances seem to be create only. Why isn't there a method to remove logger instances? It is quite nontrivial to define the semantics of a "removed" logger which is still referenced by the user.

Source: https://logging.apache.org/log4net/release/faq.html#Logger_instances_seem_to_be_create_only._Why_isnt_there_a_method_to_remove_logger_instances

like image 26
Matthew Czarnek Avatar answered Sep 15 '25 17:09

Matthew Czarnek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!