我正在使用 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>
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);
但要么它不使用我指定的模式,要么如果我取消注释“configType”行,它根本不显示任何内容。我只是想选择我想要的布局,因此当我调试测试时,我可以在 NUnit 运行程序中看到日志输出(文本输出)。
请注意,Common.Logging 附带了一堆默认日志适配器 - 其中一个是控制台附加器。
使用此功能无需在测试组件中维护记录器配置。
配置是测试夹具中的一个衬垫:
Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)