防止 Log4Net 写入 NUnit 文本输出窗口
当我运行单元测试时,我使用 _.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于 log4net.sourceforge.net 的一些快速研究,下面的代码片段启用了 NUnit 也将拾取的调试输出 (System.Diagnostics.Debug)。如果删除
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.