是否可以在 log4net 配置中使用通配符记录器名称?

发布于 2024-08-16 19:38:23 字数 447 浏览 4 评论 0原文

在我的应用程序中,我使用 log4net,所有类型都根据其类型创建自己的记录器 - 例如:

private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

在开发时,我将根记录器保留为 DEBUG,以便捕获代码中的所有日志输出。

但是,第三方组件也使用相同的方法,但每秒生成 100 条日志消息,我对这些消息都不感兴趣。

是否可以在记录器配置中使用某种通配符,以强制所有记录器仅在 WARN 处记录,例如:

 <logger name="com.thirdparty.*">
    <level value="WARN"/>
  </logger>

[上面的确切示例,使用 * 不起作用]

In my application, I use log4net, with all types creating their own logger based on their type - e.g. :

private static readonly ILog Log = LogManager.GetLogger(typeof(Program));

As I am developing, I leave the root logger on DEBUG so as to catch all log output from my code.

However, a third party component also uses this same approach, but is generating 100s of log messages a second, none of which I am interested in.

Is it possible to use some sort of wildcarding in the logger configuration, to force all their loggers to only log at WARN, e.g. :

 <logger name="com.thirdparty.*">
    <level value="WARN"/>
  </logger>

[The exact example above, using a * doesn't work]

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

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

发布评论

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

评论(2

记忆で 2024-08-23 19:38:23

您可以只指定命名空间的一部分,以便它将应用于该命名空间内的所有消息(包括嵌套的消息)。

这是我经常使用的示例:

  <root>
    <level value="FATAL" />
    <appender-ref ref="RollingFile" />
  </root>

  <logger name="MyCompany.Web" >
    <level value="WARN" />
    <appender-ref ref="WebErrors" />
  </logger>

  <!-- Will log all FATALs from NHibernate, including NHibernate.SQL and all the nested -->
  <logger name="NHibernate" >
    <level value="FATAL" />
  </logger>

另外,我建议阅读手册。它提供了很多解释。例如,您可以阅读Logger Hierarchy。这是那里的引用:

据说伐木工是
另一个记录器,如果其名称后跟
点是后代的前缀
记录器名称。据说记录器是
子记录器的父级(如果有)
自身与对象之间没有祖先
伐木工的后代。层次结构有效

.NET 中的命名空间和类层次结构。

还有:

级别继承:
给定记录器 X 的继承级别等于第一个记录器
记录器中的非空级别
层次结构,从 X 开始,
在层次结构中向上进行
朝向根记录器。

You can just specify part of a namespace so it will apply to all messages within that namespace (including nested).

Here is the example I often use:

  <root>
    <level value="FATAL" />
    <appender-ref ref="RollingFile" />
  </root>

  <logger name="MyCompany.Web" >
    <level value="WARN" />
    <appender-ref ref="WebErrors" />
  </logger>

  <!-- Will log all FATALs from NHibernate, including NHibernate.SQL and all the nested -->
  <logger name="NHibernate" >
    <level value="FATAL" />
  </logger>

Additionally I would recommend to read the manual. It provides a lot of explanation. For example you can read about Logger Hierarchy. Here is the quote from there:

A logger is said to be an ancestor of
another logger if its name followed by
a dot is a prefix of the descendant
logger name. A logger is said to be a
parent of a child logger if there are
no ancestors between itself and the
descendant logger. The hierarchy works
very much in the same way as the
namespace and class hierarchy in .NET.

and also:

Level Inheritance:
The inherited level for a given logger X, is equal to the first
non-null level in the logger
hierarchy, starting at X and
proceeding upwards in the hierarchy
towards the root logger.

夏の忆 2024-08-23 19:38:23

你不能做与你要求相反的事情吗?我的意思是将默认日志级别设置为“警告”,然后将您定义的特定记录器设置为“调试”。

此外,您可以将附加程序的阈值设置为“调试”,并让其他附加程序设置“警告”。

例如:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <applicationName value="Application" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <threshold value="WARN" />
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Notification" />
    <smtpHost value="server01" />
    <bufferSize value="1" />
    <lossy value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
    </layout>
    <threshold value="DEBUG" />
</appender>

Can't you do the opposite of what you're asking. What I mean is just set the default log level to warn and then set the specific loggers you have defined to DEBUG.

Also, you could set the threshold of your appender to DEBUG and have the other appender set the WARN.

For example:

<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
    <applicationName value="Application" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
    <threshold value="WARN" />
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
    <to value="[email protected]" />
    <from value="[email protected]" />
    <subject value="Notification" />
    <smtpHost value="server01" />
    <bufferSize value="1" />
    <lossy value="false" />
    <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
    </layout>
    <threshold value="DEBUG" />
</appender>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文