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

发布于 2024-08-18 15:24:39 字数 791 浏览 13 评论 0原文

有谁知道Common.Logging(对于.Net)中是否有等效项来设置log4net工厂适配器的属性?我在使用 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>

并设置如下属性:log4net.GlobalContext.Properties["BrokerID"] = 10

我最终得到的文件如下所示:Log_(null).txt 当使用 common.logging 动态连接 log4net 时。

Does anyone know if there is an equivalent in Common.Logging (for .Net) to set properties for the log4net factory adapter? I have had great success when just using log4net by doing:

<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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梓梦 2024-08-25 15:24:39

请参阅我对上一个问题的答案。也许会有帮助,也许没有。

总结一下:

  1. Common.Logging (NET) 在其网站上表示,对“上下文”的支持是计划在“下一个”版本中发布。

  2. 网站上并不清楚下一个版本(当前版本是 2.0)的计划时间。网站上写着“六月”。当前版本 (2.0) 是 2009 年 4 月。该网站上次更新是在 2009 年 5 月(也许是为了宣布 2.0)? “六月”是什么意思? 2009 年 6 月? 2010 年 6 月?两者都来了又去了。

  3. 鉴于 Common.Logging 中尚不支持“上下文”,请查看 Castle 项目中的“上下文”实现(log4net 实现为 此处)。将该实现移植到 Common.Logging 并不困难。一个风险是,最终来自 Common.Logging 的上下文实现可能与 Castle 实现不相似。

  4. Castle“上下文”支持是在 ILog/ILogger 接口上实现的。因此,不要像这样设置上下文:

通过直接 log4net 访问上下文:

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

通过日志抽象访问上下文:

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

从拥有记录器时设置上下文的角度来看,这似乎相当不错。如果您只想在没有记录器的情况下设置上下文,可能不太好。如果 Common.Logging.LogManager 知道什么抽象是“活动的”(它应该知道,因为抽象可以通过 LogManager.Adapter 属性设置/获取)。因此,也许“上下文”可以从 ILog 接口以及 LogManager.Adapter 接口获得。

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.

走野 2024-08-25 15:24:39

我分叉了 Common.Logging 并添加了此功能。

请参阅 GitHub 项目NuGet

我还提交了一个拉取请求,将更改返回到主分支/项目。

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.

弥繁 2024-08-25 15:24:39

我认为通过 common.logging 来做到这一点没有意义,因为 common.logging 旨在为实际日志实现提供一个外观,以便您可以切换log4net、NLog、EntLib 等之间的交互,无需更改应用程序代码(而只是更改配置)。请注意,common.logging 为您提供了记录器 (ILog) 的外观,但不提供记录接收器(log4net 术语中的附加程序)的外观。因此,即使您可以配置 BrokerID 属性,也不清楚如何在其他日志记录后端中使用它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文