使用 common.logging 动态设置 log4net 属性 [英] Dynamically setting a log4net property using common.logging

查看:25
本文介绍了使用 common.logging 动态设置 log4net 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

见我的 回答 上一个问题.也许它会有所帮助,也许不会.

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

总结:

  1. Common.Logging (NET) 在其网站上表示,对上下文"的支持是计划用于下一个"版本.

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

下一个版本(当前版本是 2.0) 已安排.网站上写着六月".当前版本 (2.0) 是 2009 年 4 月.该网站上次更新是在 2009 年 5 月(可能会宣布 2.0)?六月"是什么意思?2009 年 6 月?2010 年 6 月?两个人来来去去.

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.

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

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.

Castle上下文"支持在 ILog/ILogger 接口上实现.所以,而不是像这样设置上下文:

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

通过直接 log4net 访问上下文:

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;

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

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.

这篇关于使用 common.logging 动态设置 log4net 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆