log4net:配置忽略来自特定类的消息

发布于 2024-10-28 05:04:11 字数 313 浏览 2 评论 0原文

有没有办法让 log4net 配置忽略特定的类?比如我们一般会在每个类中创建一个日志。与此类似:

private static readonly ILog Log = log4net.LogManager.GetLogger("MyClass");

问题是 MyClass 记录了大量数据,并且很难找到有关其他类的信息。它是另一个使用 MyClass 的开发人员,因此我不能直接进入并更改日志文件,但在我的环境中我想忽略这些。

我可以设置我的配置文件来忽略来自特定类的消息吗?

Is there a way to have the log4net configuration ignore a specific class? For example, we generally create a log in every class. Similar to this:

private static readonly ILog Log = log4net.LogManager.GetLogger("MyClass");

The problem is MyClass logs an extreme amount of data and it gets hard to find information about other classes. Its another dev that uses MyClass so I cannot just go in and change around the log files, but in my environment I would like to ignore these.

Can I set my configuration file to ignore the messages from a specific class?

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

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

发布评论

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

评论(5

一页 2024-11-04 05:04:12

当然,使用 过滤器。

这里是发布在博客上的代码片段,以供将来参考 - 全部归功于该博客文章的作者:

<filter type="log4net.Filter.LoggerMatchFilter">
  <!-- allows this sub-namespace to be logged... -->
  <loggerToMatch value="Noisy.Namespace.But.Important" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
  <!-- ...but not the rest of it -->
  <loggerToMatch value="Noisy.Namespace" />
  <acceptOnMatch value="false" />
</filter>

Sure, use a filter.

Here's the snippet posted on the blog, for future reference - all credit to the author of that blog post:

<filter type="log4net.Filter.LoggerMatchFilter">
  <!-- allows this sub-namespace to be logged... -->
  <loggerToMatch value="Noisy.Namespace.But.Important" />
</filter>
<filter type="log4net.Filter.LoggerMatchFilter">
  <!-- ...but not the rest of it -->
  <loggerToMatch value="Noisy.Namespace" />
  <acceptOnMatch value="false" />
</filter>
情深已缘浅 2024-11-04 05:04:12

过滤器当然可以工作,但我更愿意像这样直接关闭记录器(或记录器层次结构):

<logger name="YourNameSpace.WithNoLogging" additivity="false">
    <level value="OFF" />        
</logger>
<logger name="MyClass" additivity="false">
    <level value="OFF" />        
</logger>
<root>
    <level value="ALL" />
    <appender-ref ref="YourAppender" />
</root>

假设 YourNameSpace.WithNoLogging 是一个命名空间,那么显示的配置将禁用整个命名空间上的日志记录。第二个“示例”关闭您的班级的日志记录(根据您的问题)。

A filter certainly works but I would prefer to turn off the logger (or logger hierarchy) directly like this:

<logger name="YourNameSpace.WithNoLogging" additivity="false">
    <level value="OFF" />        
</logger>
<logger name="MyClass" additivity="false">
    <level value="OFF" />        
</logger>
<root>
    <level value="ALL" />
    <appender-ref ref="YourAppender" />
</root>

Assuming that YourNameSpace.WithNoLogging is a namespace then the shown configuration would disable logging on the entire name space. The second "example" turns off logging for your class (according to your question).

友谊不毕业 2024-11-04 05:04:12

我建议也使用 过滤器 。然而,由于我在尝试实现过滤器时很难找到整个情况,所以我发布了我创建的配置文件的示例片段,它指出了过滤器的去向。

在这种情况下,您要使用的过滤器是

log4net.Filter.LoggerMatchFilter ----(与开头匹配
记录器名称。)

提示:Log4Netconfig文件中,放置标签的位置很重要,而且它们的优先级实际上很重要。因此,在本例中 标记位于 开始标记之后、< /代码> 标签。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="RollingFile.PassedDevices" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LoggerMatchFilter">
                <loggerToMatch value="Foo.namespace.bar.mySubclass" />
                <acceptOnMatch value="false" />
            </filter>
            <file value="myPassedDevices.log" />
            <appendToFile value="true" />
            <maximumFileSize value="100KB" />
            <maxSizeRollBackups value="2" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%timestamp    %level  - %message  [%thread]       %logger%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingFile" /> <!-- My other appender which logs all and I cut it out in this snippet. Remember that you should reference all your appenders in this tag to make them work.-->
            <appender-ref ref="RollingFile.PassedDevices" />
        </root>
    </log4net>
</configuration>

在这种技术中,您可以拥有多个 appender,您可以将特定记录器的日志记录结果重定向到单独的 appender,而不是忽略它们。例如,一个用于所有日志的appender,一个用于特定class 的已过滤日志。

I would suggest using Filters as well. However, since I struggled finding the whole picture when I was trying to implement the filter I am posting a sample snippet of the Configutation file I created which points out where filters go.

The filter you are going for in this case would be

log4net.Filter.LoggerMatchFilter ----(Matches against a the start of the
logger name.)

Hint: In the config file for Log4Net it is important where you put your tags and the priority of them actually matters. So in this case <filter> tag comes after the <appender>opening tag and before it's <file value = ... /> tag.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <log4net>
        <appender name="RollingFile.PassedDevices" type="log4net.Appender.RollingFileAppender">
            <filter type="log4net.Filter.LoggerMatchFilter">
                <loggerToMatch value="Foo.namespace.bar.mySubclass" />
                <acceptOnMatch value="false" />
            </filter>
            <file value="myPassedDevices.log" />
            <appendToFile value="true" />
            <maximumFileSize value="100KB" />
            <maxSizeRollBackups value="2" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%timestamp    %level  - %message  [%thread]       %logger%newline" />
            </layout>
        </appender>
        <root>
            <level value="DEBUG" />
            <appender-ref ref="RollingFile" /> <!-- My other appender which logs all and I cut it out in this snippet. Remember that you should reference all your appenders in this tag to make them work.-->
            <appender-ref ref="RollingFile.PassedDevices" />
        </root>
    </log4net>
</configuration>

In this technique you can have multiple appenders which you can redirect the logging results of a specific logger to a separate appender instead of ignoring them. Such as one appender for all logs and one for the filtered out logs for a specific class.

¢好甜 2024-11-04 05:04:12

您可能想尝试将类别应用于您自己的消息
试试这个线程:
如何向 log4net 消息添加类别前缀?

You might want to try to apply a category to your own messages
Try this thread:
How to add category prefix to log4net message?

So要识趣 2024-11-04 05:04:12

只是想发布如果您使用 NHibernate 和 log4net 时常见的排除情况。请注意,每个过滤器都需要自己的元素。

<filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.Engine.StatefulPersistenceContext.ProxyWarnLog" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.LazyInitializationException" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.Cache.NoCacheProvider" />
    <acceptOnMatch value="false" />
  </filter>

Just wanted to post the common exclusions if you are using NHibernate and log4net. Note that each filter needs its own element.

<filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.Engine.StatefulPersistenceContext.ProxyWarnLog" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.LazyInitializationException" />
    <acceptOnMatch value="false" />
  </filter>
  <filter type="log4net.Filter.LoggerMatchFilter">
    <loggerToMatch value="NHibernate.Cache.NoCacheProvider" />
    <acceptOnMatch value="false" />
  </filter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文