网上大篇的教程教人怎么使用log4net,大部分是用配置文件,当然也有用代码的。能解决大部分需求。
但是!!怎么在代码里创建隔离的logger 呢,就是说这个logger 只使用某一个appender,跟其他的logger 互不影响。
Create a log4net logger in code
I found myself requiring to instantiate a log4net logger in code (not through the xml configuration).
However, there are a few things that I haven’t worked around yet, here’s the code I came up with.
Note that setting the level needs to be both on the hierarchy and the logger. This is based on the log4net hierarchy implementation.
Creating the Appenders is quite simple.
The one thing I could not get to work is accessing the logger through the default way for accessing a logger.
1 LogManager.GetLogger("logname");
This action will create a new logger, with the same name, but will not use my registered version. This means the logger has no appenders registered.
If anyone knows how this can be accomplished, please let me know.
My actual goals was to accomplish following xml configuration through code where I would only configure some appenders to a specific logger without needing to add them to the root.