log4net:如何关闭程序集引用的日志记录



在我的项目中,我使用 log4net 进行日志记录。我的项目引用了一个DLL,它也使用log4net进行日志记录。现在我面临的问题是,我的日志文件有两个输出:来自我自己的代码的消息和来自引用的消息。

如何关闭引用的 DLL 的日志记录?

除了 Ash Burlaczenko 的回答之外,您还可以从外部命名空间中过滤这些消息,如下所示:

<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>

(从此处复制的 XML)

您需要仅为命名空间创建一个记录器

<logger name="YourNamespace.SubNamespace">
    <appender-ref ref="YourAppender" />
</logger>

然后,只有该命名空间中的日志记录才会发送到 YourAppender。

最新更新