以编程方式配置 Log4NetLoggerFactoryAdapter
我正在使用 NUnit 来测试一个项目,我想配置我的测试以编程方式设置 Common.Logging 以使用 Log4Net。这是我尝试过的:
NameValueCollection config = new NameValueCollection();
//config.Add("configType", "EXTERNAL");
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DevelopMENTALMadness.Data.Sql.Tests.loggerconfig.xml");
XmlConfigurator.Configure(stream);
LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);
使用以下文件:
<log4net>
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] %-4timestamp %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
但
NameValueCollection config = new NameValueCollection();
//config.Add("configType", "EXTERNAL");
var x = new ConsoleAppender { Layout = new PatternLayout("[%thread] %-4timestamp %-5level %logger %ndc - %message%newline") };
BasicConfigurator.Configure(x);
LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);
要么它不使用我指定的模式,要么如果我取消注释“configType”行,它根本不显示任何内容。我只是想选择我想要的布局,因此当我调试测试时,我可以在 NUnit 运行程序中看到日志输出(文本输出)。
I am using NUnit to test a project and I'd like to configure my tests to setup Common.Logging programmatically to use Log4Net. Here's what I've tried:
NameValueCollection config = new NameValueCollection();
//config.Add("configType", "EXTERNAL");
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DevelopMENTALMadness.Data.Sql.Tests.loggerconfig.xml");
XmlConfigurator.Configure(stream);
LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);
With the following file:
<log4net>
<appender name="A1" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%thread] %-4timestamp %-5level %logger %ndc - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to A1 -->
<root>
<level value="DEBUG" />
<appender-ref ref="A1" />
</root>
And
NameValueCollection config = new NameValueCollection();
//config.Add("configType", "EXTERNAL");
var x = new ConsoleAppender { Layout = new PatternLayout("[%thread] %-4timestamp %-5level %logger %ndc - %message%newline") };
BasicConfigurator.Configure(x);
LogManager.Adapter = new Log4NetLoggerFactoryAdapter(config);
But either it doesn't use the pattern I specify or if I uncomment the "configType" line it displays nothing at all. I'm just trying to select the layout I want so when I'm debugging my tests I can see the log output in the NUnit runner (Text Output).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,Common.Logging 附带了一堆默认日志适配器 - 其中一个是控制台附加器。
使用此功能无需在测试组件中维护记录器配置。
配置是测试夹具中的一个衬垫:
Just a heads up, Common.Logging comes with a bunch of default log adapters - one of which is a console appender.
Using this removes the need for maintaining logger configuration in test assemblies.
Configuration is a one liner in your test fixture:
这就是我最终所做的 - 它满足了我想要在 NUnit 运行器控制台中查看输出的目标,而且我最终还添加了一个滚动文件记录器。
在测试类中:
然后我有一个 App.config 文件(始终复制):
So here's what I ended up doing - it meets my goal of wanting to see the output in NUnit runner console, plus I ended up adding a rolling file logger as well.
In the test class:
Then I have an App.config file (copy always):