根据情况在log4net的不同的appender记录 [英] logging in log4net to different appenders based on circumstances

查看:153
本文介绍了根据情况在log4net的不同的appender记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用log4net的,并在一类要求记录到RollingFile附加目的地,但随后在另一个类,我希望记录到事件日志+滚动文件+控制台附加目的地。

I am using log4net and in one class require logging to a RollingFile appender, but then in another class, I wish to log to the event log + rolling file + console appender.

什么是最好的做法是什么?而我能看到一些样品code?

What is the best practice? and could I see some sample code?

通过使事情变得更加困难,我使用的温莎城堡日志记录工具的使用log4net的解决我的Logger实例。

By the way to make things more difficult, I am using Castle Windsor Logging Facility with Log4net to resolve my Logger instance.

如果有帮助,下面我在想这一点,但不知道这是不是最好的做法,或如何根据名称仍然温莎利用我目前的记录器实例,启动特定的记录:

If it helps, I was thinking this below, but have no idea if this is best practice, or how to activate a particular logger based on 'name' still utilising my current logger instance from windsor:

log4net.config:

...
    <logger name="EventLogOnly">
      <level value="ALL" />
      <appender-ref ref="EventLogAppender" />
    </logger>
    <logger name="ConsoleEventLog">
      <level value="ALL" />
      <appender-ref ref="ColoredConsoleAppender" />
      <appender-ref ref="EventLogAppender" />
    </logger>
...

温莎城堡容器生成器类:

container.AddFacility("logging.facility", 
   new LoggingFacility(LoggerImplementation.Log4net, "log4net.config"));

类,其中记录:

private ILogger Logger;
public Test(ILogger logger) {
  Logger.Info("Can I log under event log only?");
  Logger.Info("Now can I log under both?");
}

谢谢你们。

推荐答案

您可以通过应用过滤器,以一个appender做到这一点。只有当日志事件通过过滤器并在事件得到由该附加器记录下来。

You can do this by applying a filter to an appender. Only if the log event passes the filter does the event get logged by that appender.

此过滤器的配置将只记录那些事件从一个名为MyLogger记录器来了:

This filter configuration will log only those events coming from the logger named "MyLogger":

<appender name="EventLogAppender" ...
    <filter type="log4net.Filter.LoggerMatchFilter">
        <loggerToMatch value="MyLogger" />
    </filter>   	
    <filter type="log4net.Filter.DenyAllFilter" />
</appender>

...而这一次将匹配记录与特定包含的短信:

...and this one will match log messages with certain contained text:

<filter type="log4net.Filter.StringMatchFilter">
    <stringToMatch value="database" />
</filter>
<filter type="log4net.Filter.DenyAllFilter" />

有配置可能带过滤器的好一点。请参阅 log4net的SDK ,或manual ,了解更多信息。

There's a good bit of configuration possible with filters. See the log4net SDK, or the Filters section of the manual, for more details.

这篇关于根据情况在log4net的不同的appender记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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