Q:

Configuring log4net in unit tests to log to console and display proper date and time

I am running some unit tests, and I would like log4net to log everything to the Console (just for inspection). Following the manual, I used:
BasicConfigurator.Configure();

However, this sets up the logging pattern to %-4timestamp [%thread] %-5level %logger %ndc - %message%newline. How can I change it so that instead of the timestamp I get the usual date and time (up to miliseconds)?

A:

You can get ConsoleAppender from logger repository and change it's layout:
BasicConfigurator.Configure();
var appender = LogManager.GetRepository()
                         .GetAppenders()
                         .OfType<ConsoleAppender>()
                         .First();

appender.Layout = new PatternLayout("%d %-5level %logger - %m%n"); // set pattern
ILog logger = LogManager.GetLogger(logger_name); // obtain logger

But configuration via config file is preferred way, because you can change settings without recompiling your tests.

