I have a NUnit test that uses log4net and produces a nice log as below:
[2017-07-20 INFO] Test children 00: Started
[2017-07-20 INFO] Test children 00: Log line 1
[2017-07-20 INFO] Test children 00: Log line 2
[2017-07-20 INFO] Test children 00: Log line 3
[2017-07-20 INFO] Test children 00: Completed
If I create multiple children of this test (using TestCaseSource) and run them in parallel the log gets ugly, the lines from different test children are mixed.
I would like to see something like this (please notice all logs coming from a test children are grouped together)
[2017-07-20 INFO] Test children 00: Started
[2017-07-20 INFO] Test children 00: Log line 1
[2017-07-20 INFO] Test children 00: Log line 2
[2017-07-20 INFO] Test children 00: Log line 3
[2017-07-20 INFO] Test children 00: Completed
[2017-07-20 INFO] Test children 01: Started
[2017-07-20 INFO] Test children 01: Log line 1
[2017-07-20 INFO] Test children 01: Log line 2
[2017-07-20 INFO] Test children 01: Log line 3
[2017-07-20 INFO] Test children 01: Completed
And actually I get something like this (please notice logs coming from different test children are mixed)
[2017-07-20 INFO] Test children 00: Started
[2017-07-20 INFO] Test children 01: Started
[2017-07-20 INFO] Test children 00: Log line 1
[2017-07-20 INFO] Test children 01: Log line 1
[2017-07-20 INFO] Test children 00: Log line 2
[2017-07-20 INFO] Test children 01: Log line 2
[2017-07-20 INFO] Test children 01: Log line 3
[2017-07-20 INFO] Test children 00: Log line 3
[2017-07-20 INFO] Test children 01: Completed
[2017-07-20 INFO] Test children 00: Completed
Is there a way to configure log4net to keep the logs coming from one thread together?
I'd avoid depending on an external component for logging during unit tests. It's best to avoid relying on any external dependencies during unit tests as it means your test isn't isolated to the unit under test.
If you're providing an ILog
to your unit under test, instead of providing a log4net implementation, provide a stubbed implementation that may just write to the console (Console.WriteLine
, etc). Everything written to the console during a test will be visible post-test and associated with that test.
If you HAVE to use log4net for some reason, then use the ConsoleAppender
instead of the FileAppender
in your log4net configuration.