简体   繁体   English

使用common.logging动态设置log4net属性

[英]Dynamically setting a log4net property using common.logging

Does anyone know if there is an equivalent in Common.Logging (for .Net) to set properties for the log4net factory adapter? 有没有人知道Common.Logging(for .Net)中是否有等效的设置log4net工厂适配器的属性? I have had great success when just using log4net by doing: 通过以下方式使用log4net时我取得了很大的成功:

<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="logs\Log_%property{BrokerID}.txt"/>
    <appendToFile value="false"/>
    <rollingStyle value="Size"/>
    <maxSizeRollBackups value="-1"/>
    <maximumFileSize value="50GB"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger - %message%newline"/>
    </layout>
</appender>

and setting the property like: log4net.GlobalContext.Properties["BrokerID"] = 10 并设置属性如: log4net.GlobalContext.Properties["BrokerID"] = 10

The file I end up with the looks like this: Log_(null).txt when using the common.logging to wire up log4net on the fly. 我最终得到的文件如下所示:当使用common.logging动态连接log4net时, Log_(null).txt

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. Common.Logging(NET)在其网站上表示,计划在“下一个”版本中支持“上下文”。

  2. It is not clear on the website when the next release (the current release is 2.0) is scheduled. 当下一个版本(当前版本是2.0)被安排在网站上时,目前尚不清楚。 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)? 当前版本(2.0)是2009年4月。该网站最后更新于2009年5月(也许是宣布2.0)? What does "June" mean? “六月”是什么意思? June 2009? 2009年6月? June 2010? 2010年6月? 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 ). 鉴于Common.Logging中尚未提供“上下文”支持,请查看Castle项目中的“上下文”实现(log4net实现在此处 )。 It would not be hard to port that implementation to Common.Logging. 将该实现移植到Common.Logging并不困难。 A risk is that the context implementation that eventually comes from Common.Logging might not be similar to the Castle implementation. 风险在于最终来自Common.Logging的上下文实现可能与Castle实现不相似。

  4. The Castle "context" support is implemented on the ILog/ILogger interface. Castle“context”支持在ILog / ILogger接口上实现。 So, rather than setting the context like this: 所以,而不是像这样设置上下文:

Access to context via straight log4net: 通过直接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). 如果Common.Logging.LogManager知道什么抽象是“活动的”(它应该是因为抽象是通过LogManager.Adapter属性设置/获取的)。 So, maybe the "context" could be available from the ILog interface as well as from the LogManager.Adapter interface. 因此,也许可以从ILog接口以及LogManager.Adapter接口获得“上下文”。

I forked Common.Logging and added this functionality. 我分叉了Common.Logging并添加了这个功能。

See GitHub project or NuGet . 请参阅GitHub项目NuGet

I also submitted a pull request for returning the changes to the main branch/project. 我还提交了一个pull请求,用于将更改返回到主分支/项目。

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). 我认为通过common.logging执行此操作并common.logging ,因为common.logging旨在提供实际日志记录实现的外观,以便您可以在不更改应用程序代码的情况下在log4net,NLog,EntLib等之间切换(但只是改变配置)。 Note that common.logging gives you a facade over loggers ( ILog ) but not over logging sinks (appenders in log4net parlance). 请注意, common.logging为您提供了loggers( ILog )的外观,但没有通过日志记录接收器(log4net用语中的appender)。 So even if you could configure a BrokerID property, it's not clear how this might be used in the other logging backends. 因此,即使您可以配置BrokerID属性,也不清楚如何在其他日志记录后端中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM