Dynamically setting a log4net property using common.logging

空扰寡人 提交于 2019-12-05 22:04:40
wageoghe

See my answer to a previous question. Maybe it will help, maybe not.

To summarize:

  1. Common.Logging (NET) says on its website that support for "context" is planned for the "next" release.

  2. It is not clear on the website when the next release (the current release is 2.0) is scheduled. The website says "June". The current release (2.0) was April 2009. The website was last updated in May 2009 (maybe to announce 2.0)? What does "June" mean? June 2009? June 2010? Both have come and gone.

  3. Given that "context" support is not available yet in Common.Logging, take a look at the "context" implementation in the Castle project (log4net implementation is here). It would not be hard to port that implementation to Common.Logging. A risk is that the context implementation that eventually comes from Common.Logging might not be similar to the Castle implementation.

  4. The Castle "context" support is implemented on the ILog/ILogger interface. So, rather than setting the context like this:

Access to context via straight log4net:

log4net.GlobalContext.Properties["BrokerID"] = 10;

Access to context via logging abstraction:

ILog logger = Common.Logging.LogManager.GetCurrentClassLogger();
logger.GlobalContext.Properties["BrokerID"] = 10;

This seems pretty good from the perspective of setting the context when you have a logger. Maybe not so good if you just want to set the context without having gotten a logger. If the Common.Logging.LogManager knows what abstraction is "active" (and it should because the abstraction is settable/gettable via the LogManager.Adapter property). So, maybe the "context" could be available from the ILog interface as well as from the LogManager.Adapter interface.

I forked Common.Logging and added this functionality.

See GitHub project or NuGet.

I also submitted a pull request for returning the changes to the main branch/project.

I don't think it makes sense to do this through common.logging, because common.logging is intended to provide a facade over the actual logging implementation so that you can switch between log4net, NLog, EntLib etc. without changing your application code (but just changing the configuration). Note that common.logging gives you a facade over loggers (ILog) but not over logging sinks (appenders in log4net parlance). So even if you could configure a BrokerID property, it's not clear how this might be used in the other logging backends.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!