I want to try log some app messages from my app. In this very situation I just want to force nunit to work with log4net. I found some example here http://www.ofconsulting.com/PublicPortal/ofc-tech-blog/92-configure-log4net-with-nunit.html.
log4net is confugured in app.config like this:
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
And in my test code is following
[TestFixture]
class DomainTests
{
protected static readonly ILog log = LogManager.GetLogger(typeof(DomainTests));
public void LoggingTests()
{
log4net.Config.XmlConfigurator.Configure();
}
[Test]
public void BasicLogTest()
{
log.Error("write my log entry already");
}
My test is passed but nothing is written inside log.txt file. What am I doing wrong?
I just want to make it as simple as possible to store messages like entering an application, exit application. Regards.
log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.
As long as I know and used log4net with nunit, I have never used any config file, you just need to add following line in test class constructor
BasicConfigurator.Configure();
here is the full answer if you like to see the sample test class
The problem is that the NUnit test runner (when run from resharper in visual studio) runs the test from another folder (it shadow copies the test assembly), so the xml configuration is not available at that point unless you specify the full config path.
You could of course use the basic configuration and specify the logging configuration in code, like:
log4net.Config.BasicConfigurator.Configure(
new log4net.Appender.ConsoleAppender {
Layout = new log4net.Layout.SimpleLayout()});
You should see the log output in the test output after that.
My best guess would be for you to do something like this:
[TestFixture]
class DomainTests
{
protected static readonly ILog log = LogManager.GetLogger(typeof(DomainTests));
public void LoggingTests()
{
log4net.Config.XmlConfigurator.Configure();
}
[Test]
public void BasicLogTest()
{
log.Error("write my log entry already");
}
[SetUp]
RunBeforeAnyTests()
{
BasicConfigurator.Configure();
}
[TearDown]
RunAfterAnyTests()
{
// ...
}
I'd also use the Log4Net.config file rather than the app.config file, it just seems cleaner. Here's an example log4net.config file:
<log4net>
<!-- A1 is set to be a LogFileAppender -->
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender, log4net" >
<param name="File" value="C:\logging\log.txt" />
<file value="c:\logging\Main" />
<appendToFile value="true" />
<datePattern value="yyyyMMdd'.log'" />
<rollingStyle value="Composite" />
<staticLogFileName value="false" />
<maxSizeRollBackups value="-1" />
<maximumFileSize value="500MB" />
<!-- A1 uses PatternLayout -->
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<!-- Set root logger level to DEBUG and its only appender to LogFileAppender -->
<root>
<!--<level value="OFF" />-->
<!--<level value="FATAL" />-->
<!--<level value="ERROR" />-->
<!--<level value="WARN" />-->
<!--<level value="INFO" />-->
<level value="DEBUG" />
<!--<level value="ALL" />-->
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With