防止 Log4Net 写入 NUnit 文本输出窗口

发布于 2024-10-10 22:16:20 字数 7324 浏览 2 评论 0原文

当我运行单元测试时,我使用 _.P("My Test Message"); 将信息打印到 NUnit 中的文本输出窗口。

但我的消息被 Log4Net 消息淹没了。

我想从 NUnit 文本输出中删除 Log4Net 消息。

Log4Net 配置为仅写入文件。

<log4net debug="true">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <param name="DatePattern" value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d - %m%newline" />
    </layout>
  </appender>

  <root>
    <level value="All" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

第一次运行单元测试时,我得到以下输出

log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [All].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [All].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: XmlHierarchyConfigurator: Loading Appender [RollingLogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Loading Appender [RollingLogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [Log.txt]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [Log.txt]
log4net: XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [10MB]
log4net: XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [10MB]
log4net: XmlHierarchyConfigurator: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMMdd]
log4net: XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMMdd]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%-5p %d - %m%newline]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%-5p %d - %m%newline]
log4net: PatternParser: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [d] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [d] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: RollingFileAppender: Searched for existing files in [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest]
log4net: RollingFileAppender: Searched for existing files in [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: FileAppender: Opening file for writing [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest\Log.txt] append [True]
log4net: FileAppender: Opening file for writing [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest\Log.txt] append [True]
log4net: XmlHierarchyConfigurator: Created Appender [RollingLogFileAppender]
log4net: XmlHierarchyConfigurator: Created Appender [RollingLogFileAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [RollingLogFileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Adding appender named [RollingLogFileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []

在后续单元测试执行中,我得到以下输出。

log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].

When I run unit tests, I use _.P("My Test Message"); to print information to the Text Output window in NUnit.

But my messages are being overwhelmed by Log4Net messages.

I would like to remove the Log4Net messages from my NUnit text output.

Log4Net is configured to write to a file only.

<log4net debug="true">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="Log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="10MB" />
    <staticLogFileName value="true" />
    <param name="DatePattern" value="yyyyMMdd" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%-5p %d - %m%newline" />
    </layout>
  </appender>

  <root>
    <level value="All" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
</log4net>

On 1st running a Unit Test, I get the following outputs

log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [All].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [All].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: XmlHierarchyConfigurator: Logger [root] level set to [name="ALL",value=-2147483648].
log4net: XmlHierarchyConfigurator: Loading Appender [RollingLogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Loading Appender [RollingLogFileAppender] type: [log4net.Appender.RollingFileAppender]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [Log.txt]
log4net: XmlHierarchyConfigurator: Setting Property [File] to String value [Log.txt]
log4net: XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [AppendToFile] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: XmlHierarchyConfigurator: Setting Property [RollingStyle] to RollingMode value [Size]
log4net: XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: XmlHierarchyConfigurator: Setting Property [MaxSizeRollBackups] to Int32 value [10]
log4net: XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [10MB]
log4net: XmlHierarchyConfigurator: Setting Property [MaximumFileSize] to String value [10MB]
log4net: XmlHierarchyConfigurator: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [StaticLogFileName] to Boolean value [True]
log4net: XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMMdd]
log4net: XmlHierarchyConfigurator: Setting Property [DatePattern] to String value [yyyyMMdd]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [message] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%-5p %d - %m%newline]
log4net: XmlHierarchyConfigurator: Setting Property [ConversionPattern] to String value [%-5p %d - %m%newline]
log4net: PatternParser: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [p] Option [] Format [min=5,max=2147483647,leftAlign=True]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [d] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [d] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [literal] Option [ - ] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [m] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: PatternParser: Converter [newline] Option [] Format [min=-1,max=2147483647,leftAlign=False]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: XmlHierarchyConfigurator: Setting Property [Layout] to object [log4net.Layout.PatternLayout]
log4net: RollingFileAppender: Searched for existing files in [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest]
log4net: RollingFileAppender: Searched for existing files in [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: RollingFileAppender: curSizeRollBackups starts at [0]
log4net: FileAppender: Opening file for writing [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest\Log.txt] append [True]
log4net: FileAppender: Opening file for writing [D:\Development\Source\V3.0\SiteMajik.Generators\SiteMajik.Generators.UnitTest\Log.txt] append [True]
log4net: XmlHierarchyConfigurator: Created Appender [RollingLogFileAppender]
log4net: XmlHierarchyConfigurator: Created Appender [RollingLogFileAppender]
log4net: XmlHierarchyConfigurator: Adding appender named [RollingLogFileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Adding appender named [RollingLogFileAppender] to logger [root].
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []
log4net: XmlHierarchyConfigurator: Hierarchy Threshold []

On subsequent Unit Test executions I get the following output.

log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].
log4net:ERROR [TextWriterAppender] Attempted to append to closed appender named [].

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

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

发布评论

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

评论(1

葬花如无物 2024-10-17 22:16:20

基于 log4net.sourceforge.net 的一些快速研究,下面的代码片段启用了 NUnit 也将拾取的调试输出 (System.Diagnostics.Debug)。如果删除 debug="true",您应该会得到您想要的行为。

<log4net debug="true">

Based on some quick research at log4net.sourceforge.net, this snippet below enables the debug output (System.Diagnostics.Debug) which NUnit will also pick up. If you remove the debug="true", you should get the behavior you want.

<log4net debug="true">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文