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).
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:
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):