NUnit and Log4Net

247 views
Skip to first unread message

rbr

unread,
Apr 2, 2009, 10:56:26 AM4/2/09
to NUnit-Discuss
Hello all,

I have an issue getting coverage using NUnit on my logging code.
For example, I have a lot of code that looks like the following:

public class FuBar
{

private static ILog log = LogManager.GetLogger(typeof(FuBar));

public FuBar(){}

public int AddSomething(int a, int b)
{
if(log.IsDebugEnabled)
{
log.Debug("This code is never hit during unit
testing);
}
return a + b;
}
}

When I write an NUnit test for this example, I will get coverage for
every line but the log.Debug...

Does anybody have any suggestions for this? I tried to add an
app.config with the log4net settings to the test project. However, for
some odd reason, it only seems to work inconsistently. In addition,
our build guys prefer that we don't add app.config files to test
projects.

Thank you in advance for any help.

rbr

Charlie Poole

unread,
Apr 2, 2009, 11:48:50 AM4/2/09
to nunit-...@googlegroups.com
Hi,

> I have an issue getting coverage using NUnit on my logging code.
> For example, I have a lot of code that looks like the following:
>
> public class FuBar
> {
>
> private static ILog log = LogManager.GetLogger(typeof(FuBar));
>
> public FuBar(){}
>
> public int AddSomething(int a, int b)
> {
> if(log.IsDebugEnabled)
> {
> log.Debug("This code is never hit during unit
> testing);
> }
> return a + b;
> }
> }
>
> When I write an NUnit test for this example, I will get
> coverage for every line but the log.Debug...

... which, of course, indicates that IsDebugEnabled is false.
If you want to test that code, then you need to enable it in
some way- either in your test code or via some config, as you
are trying to do.

> Does anybody have any suggestions for this? I tried to add an
> app.config with the log4net settings to the test project.
> However, for some odd reason, it only seems to work
> inconsistently. In addition, our build guys prefer that we
> don't add app.config files to test projects.

When you say an "App.config" do you mean, literally, a file
of that name like you use in a VS exe project? Usually,
tests are dlls and VS will not do a copy/rename of those
files. Have you read the section of the docs about how
to set up a config file for your tests?

Of course, if your "build guys" are unhappy with needing
to deploy a test.dll.config with your test.dll, that's
a problem. Apparently, they are not aware of who is
providing a service to whom in your relationship. :-)

Charlie

rbr

unread,
Apr 2, 2009, 12:04:15 PM4/2/09
to NUnit-Discuss
Thanks for the quick reply.

Debugging is enabled in all of my projects via the log4net settings.
However, from my understanding, IsDebugEnabled is, by default, turned
off in NUnit. That is why I came up with the solution of the
app.config. Alas, I do need to defer to the whims of our build gurus
since they set-up the scripts and don't want to have additional
dependencies to worry about.

rbr

Charlie Poole

unread,
Apr 2, 2009, 4:46:08 PM4/2/09
to nunit-...@googlegroups.com
Hi,

A lot depends on your version. Only NUnit 2.4.4 through
2.4.6 used log4net for logging, so with those releases
it's possible for NUnit to interfere with your logging.

That's why we changed as of 2.4.7 so that NUnit no longer
uses log4net. We neither disable nor enable any logging
and we use reflection to display your logs in the Gui
if your app uses log4net.

The usual way to enable logging for a test is via the
test config - which, as I said, is not the App.config.
Alternatively, you could enable/disable it programmatically
in your tests.

Charlie

rbr

unread,
Apr 2, 2009, 11:22:03 PM4/2/09
to NUnit-Discuss
How would I do this programmatically? I have heard people mention this
approach but, have yet to see an example.

Thank you so much for your help.

rbr
> > > > rbr- Hide quoted text -
>
> - Show quoted text -

Ian Chamberlain

unread,
Apr 3, 2009, 4:23:49 AM4/3/09
to NUnit-Discuss
Try

LogManager.Shutdown();
Configuration config =
ConfigurationManager.OpenMappedMachineConfiguration(new
ConfigurationFileMap(pathToCreatedTestConfigFile));
XmlConfigurator.ConfigureAndWatch(new FileInfo(config.FilePath));

where pathToCreatedTestConfigfile is the absolute path to the
configuration file you want to use for your test and contains whatever
Log4Net configuration you need..

Regards

Ian Chamberlain
> > - Show quoted text -- Hide quoted text -

rbr

unread,
Apr 13, 2009, 3:22:40 PM4/13/09
to NUnit-Discuss
Perfect! thank you!

On Apr 3, 2:23 am, Ian Chamberlain <ian.j.chamberl...@googlemail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages