Andre
Andre

Reputation: 647

log4net and NUnit 3.2 and console output in VS

In my NUnit 2.6 tests I used to see log4net log messages in the output window of Visual Studio, in the Tests section. Since I switched to NUnit 3.2, they are no longer displayed, which is very inconvenient. I tried searching and the best "solution" I came up with was to dump everything into debug strings, which can be viewed either via DebugView utility from SysInternals or when I actually debug a test - then the messages are shown in Debug section. However, I would really like to see my log lines in the Visual Studio even when not debugging. Any ideas? Visual Studio 2015. This is my current log4net config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>

  <log4net>
    <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <appender name="DebugAppender" type="log4net.Appender.DebugAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="OutputDebugStringAppender" />
      <appender-ref ref="DebugAppender" />
    </root>
  </log4net>
</configuration>

Upvotes: 3

Views: 1600

Answers (3)

Lars-Erik
Lars-Erik

Reputation: 311

For me the solution was to set the ConsoleAppender's target property to "Console.Out":

<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender, log4net">
    <target value="Console.Out"/>
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{dd MMM yyyy HH:mm:ss} %level [%thread] %method - %message%n" />
    </layout>
</appender>

Did not need to set out to TestContext.Progress.

This is with NUnit 3.11 and the R# testrunner.
PS. Noticed we're using different appenders. I'll leave this in here anyway since it's came up in my main SERP. :)

Upvotes: 1

Leontiev Evgeniy
Leontiev Evgeniy

Reputation: 11

For NUnit v3 you can do that with this line of code:

Console.SetOut(TestContext.Progress);

Log4Net configured to use ConsoleAppender.

Upvotes: 1

Charlie
Charlie

Reputation: 13736

NUnit V2 captured log4net output and translated it into an NUnit event. For 3.0, we decided this was out of scope for NUnit and left it to log4net to display things. When running under the NUnit console runner, this works fine but it turns out to be a bit of a limitation under the VS adapter.

I think it would be reasonable for us to supply either an appender or an engine extension you could use to get log4net output into the form of an NUnit output event. It's a matter of someone volunteering to write it. If you think this is important, you might file an issue on github.

Upvotes: 3

Related Questions