Trace settings for unit testing?

I’m currently working on a project to do automatic Remoting publication of XPO services, and as this is supposed to run as a service, I wanted to put in trace messages. I hope every .NET programmer is aware of the nice tracing framework integrated in the .NET framework (if I caught you here, head here and work your way through the related topics). I wanted to use a TraceSwitch instance and log messages with various levels throughout my code – this was easily done.

Then I stumbled upon a problem: in some places in my code I log with levels Info or Verbose, but the default level for the trace switch I used was only “2”, i.e. Warning. When the library is being used by an application, that’s not a problem but a feature: via the configuration section in App.config, the application developer can configure the trace level that is eventually being used by the switch. But when my unit and functional tests run (NUnit and TestDriven.NET), I would like to be able to see all the output from tracing for debugging purposes. Obviously there’s no App.config in place at this point, so I had to find another way of setting the default trace level for the switch.

I looked around extensively for a way to set the value that’s otherwise read from the config file from code – no way. Basically the whole configuration stuff, as it applies to default sections like “system.diagnostics”, is really well encapsulated in the System.Configuration assembly, so without a lot of bad reflection code it’s not possible to influence things from that side.

To make a long(ish) story short, the solution I found is this: just create an App.config file for the NUnit test assembly. What? Yes. Was complete news to me, but even though my test assembly is just a class library, it’s possible to add an App.config to it, and the configuration I put in there is used for the test run. Nice – you (I, at least) never stop learning… 🙂

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s